KatInterview

Design a Deduplicated Storage System

Data Structure DesignAuto-graded · 16 test cases
EasyAuto-gradedData Structure Designdesign-dsarraysstrings

Last updated

Simulate file system operations while maintaining deduplicated content storage using reference counting.

Design an in-memory storage manager that automatically deduplicates file contents across different file paths. When multiple files have identical content payloads, the payload should be stored efficiently so that duplicate contents consume minimal memory overhead.

You will process a sequence of file operations provided as two parallel lists: operations (the method names) and arguments (the parameter lists for each method call).

The supported operations are:

  1. write_file(path, content):

    • Saves content at path.
    • If path already exists, overwrites its content with the new payload.
    • Return value: None (represented as null in output lists).
  2. read_file(path):

    • Returns the content stored at path.
    • If path does not exist, returns None.
  3. delete_file(path):

    • Removes the file at path.
    • Returns True if the file was present and deleted, or False if path did not exist.
  4. count_unique_contents():

    • Returns the number of distinct, non-orphaned content strings currently referenced by at least one existing path.

If an operation (such as an overwrite or deletion) causes a content payload to lose its last referencing file path, that content payload must no longer be tracked or counted.

Write a function run_file_system_operations(operations: List[str], arguments: List[List[str]]) -> List[object] that processes the sequence of calls in order and returns a list containing the result of each operation.

Constraints

  • 1 <= len(operations) == len(arguments) <= 20,000
  • operations[i] is one of "write_file", "read_file", "delete_file", or "count_unique_contents".
  • Arguments match the expected signature of each command:
    • write_file: two string arguments [path, content].
    • read_file: one string argument [path].
    • delete_file: one string argument [path].
    • count_unique_contents: empty list [].
  • All string comparisons (paths and contents) are case-sensitive.
  • content may be any string, including the empty string "".

Examples

Example 1

Input: run_file_system_operations(['write_file', 'write_file', 'count_unique_contents', 'read_file', 'write_file', 'count_unique_contents', 'delete_file', 'count_unique_contents', 'read_file'], [['/a/b/c.txt', 'market_data_v1'], ['/a/b/e.txt', 'market_data_v1'], [], ['/a/b/c.txt'], ['/a/b/c.txt', 'market_data_v2'], [], ['/a/b/e.txt'], [], ['/nonexistent']])

Output: [None, None, 1, 'market_data_v1', None, 2, True, 1, None]

Hints

Hint 1

Use two hash maps: one mapping each file path to its content payload, and another mapping each content payload to its active reference count.

Hint 2

When overwriting an existing file path, remember to decrement the reference count of the old content before updating the path's pointer.

Hint 3

When a content string's reference count drops to zero, remove it from the reference counting map so that count_unique_contents remains accurate.