KatInterview

Hierarchical Storage Simulator with Auto-Renamed Duplicates

Data Structure DesignAuto-graded · 20 test cases
MediumAuto-gradedData Structure Designdesignhash-mapstring-parsing

Last updated

Simulate an in-memory directory tree with auto-created folders, numbered duplicate file names that respect extensions, content lookup, and sorted listings.

You are building a small in-memory storage tree that starts with only the root directory /. It supports four commands, and you must replay a sequence of them and report what each one returns.

Commands

Every path is absolute (it begins with /).

  • mkdir with one argument path: make every directory along path that does not exist yet, including intermediate ones. If everything already exists, nothing changes. Result: None.

  • create_file with arguments path and content:

    1. The last component of path is the requested file name, and the preceding components form the parent directory. Create any missing parent directories.
    2. If the requested name is not used by any entry (file or subdirectory) in the parent directory, store the file under that name.
    3. Otherwise, the file gets a numbered name. Try k = 1, 2, 3, ... in order and use the first k for which the numbered name is not used by any entry in that parent directory.
    4. Build the numbered name from the requested name as follows. Let d be the index of the last . in it.
      • If d > 0 (there is a dot that is not the first character), put (k) right before that last dot. For example, doc.txt becomes doc(1).txt, and a.b.c becomes a.b(1).c. A trailing dot works the same way: a. becomes a(1)..
      • If there is no dot, or the only dot is the first character, append (k) to the end. For example, data becomes data(1) and .env becomes .env(1).

    Numbering is applied to the requested name as given, so asking for a name that already looks numbered, such as notes(1).txt, is handled by the same rule (notes(1)(1).txt).

    Result: the final file name that was stored (just the name, not the full path).

  • read_file with one argument path: Result: the content of the file stored at exactly that path. The file is guaranteed to exist.

  • ls with one argument path:

    • If path is a directory, Result: the names of its direct children (files and subdirectories), sorted in plain lexicographic (code point) order.
    • If path is a file, Result: a list containing only that file's name.

Names are case-sensitive.

Your task

Implement a function that receives a list of command names and a parallel list of argument lists. Process the commands in order, starting from an empty tree, and return a list with one result per command, using the results defined above (None for mkdir).

Constraints

  • commands and params have the same length, at most 10^4.
  • commands[i] is one of "mkdir", "create_file", "read_file", "ls", and params[i] holds the arguments for that command in the order described above.
  • All paths are absolute and start with /. None ends with / except the root path / itself.
  • Path components contain only letters, digits, . and _.
  • Paths are well formed, and read_file and ls are only used on paths that exist.

Examples

Example 1

Input: simulate_storage_ops(['mkdir', 'ls', 'create_file', 'create_file', 'create_file', 'ls', 'read_file'], [['/a/b'], ['/a'], ['/a/b/notes.txt', 'first'], ['/a/b/notes.txt', 'second'], ['/a/b/notes.txt', 'third'], ['/a/b'], ['/a/b/notes(1).txt']])

Output: [None, ['b'], 'notes.txt', 'notes(1).txt', 'notes(2).txt', ['notes(1).txt', 'notes(2).txt', 'notes.txt'], 'second']

Example 2

Input: simulate_storage_ops(['create_file', 'create_file', 'create_file', 'create_file', 'ls'], [['/data', 'val1'], ['/data', 'val2'], ['/.env', 'secret'], ['/.env', 'secret2'], ['/']])

Output: ['data', 'data(1)', '.env', '.env(1)', ['.env', '.env(1)', 'data', 'data(1)']]

Hints

Hint 1

Keep two hash maps keyed by full absolute path: one from each directory to the set of its child names, and one from each file path to its content.

Hint 2

To split a requested file name for numbering, look at the last dot: only when its index is greater than 0 does it count as an extension separator. Dotfiles like `.env` and names without dots just get `(k)` appended.

Hint 3

For a collision, start `k` at 1 on every call and keep incrementing until the candidate name is absent from the parent's child set. This guarantees the smallest `k`. Don't forget that a subdirectory name also blocks a file name.