KatInterview

Design a Tree-Based Storage Simulator with Auto-Renaming

Data Structure DesignModel answer
HardModel answerData Structure Designdesigntreehash-set

Last updated

Build an in-memory file store that auto-creates parent folders, renames clashing files with the lowest free numeric tag, and lists folder contents in ASCII order.

Task

Build a small in-memory storage system shaped like a file tree. When a file is stored, any folders along its path that do not yet exist must be created automatically. If a file with the requested name is already present in the target folder, the system must pick a new name by inserting a numeric tag, using the smallest tag that gives an unused name.

Implement the class FileVault:

  • FileVault() creates a vault that contains only the root folder /.
  • add_file(path: str) -> str stores a file at the absolute path path, creating missing parent folders. It returns the absolute path under which the file was actually stored, after any renaming.
  • list_dir(path: str) -> list[str] returns the names of all direct children (subfolders and files) of the folder at path, sorted in ASCII order. If path does not exist, or points to a file, return an empty list. Listing must never create anything.

Renaming rules

Split a file name into a base and an extension:

  1. If the name contains a . at an index greater than 0, take the last such .. The extension is everything from that dot to the end, and the base is everything before it. For example, "archive.tar.gz" gives base "archive.tar" and extension ".gz".
  2. If there is no ., or the only . is at index 0 (such as ".env"), the extension is empty and the base is the whole name.
  3. If the name is already used in the target folder, try base + " (" + k + ")" + ext for k = 1, 2, 3, ... and use the first name not already in the folder.

Interface

class FileVault:
    def __init__(self):
        pass

    def add_file(self, path: str) -> str:
        pass

    def list_dir(self, path: str) -> list[str]:
        pass

Constraints

  • Every path is absolute and starts with /.
  • Paths are ASCII, contain no //, and have no trailing slash (except the root / itself).
  • Names never contain /.
  • At most 10,000 calls in total to add_file and list_dir.
  • Within one folder, a file and a subfolder never have the same name.

Examples

v = FileVault()
v.add_file("/docs/report.pdf")   # "/docs/report.pdf"
v.add_file("/docs/report.pdf")   # "/docs/report (1).pdf"
v.add_file("/docs/report.pdf")   # "/docs/report (2).pdf"
v.list_dir("/docs")
# ["report (1).pdf", "report (2).pdf", "report.pdf"]
v = FileVault()
v.add_file("/home/.bashrc")        # "/home/.bashrc"
v.add_file("/home/.bashrc")        # "/home/.bashrc (1)"
v.add_file("/home/notes (2).txt")  # "/home/notes (2).txt"
v.add_file("/home/notes.txt")      # "/home/notes.txt"
v.add_file("/home/notes.txt")      # "/home/notes (1).txt"
v.list_dir("/home")
# [".bashrc", ".bashrc (1)", "notes (1).txt", "notes (2).txt", "notes.txt"]

What to deliver

Describe your data model, explain how you pick the new name and why it yields the smallest valid tag, give the code, and state its time and space complexity. Mention the edge cases you would test.

Hints

Hint 1

Represent each folder as a node holding a map of child folders and a set of file names. Creating parents is then the same walk as `mkdir -p`.

Hint 2

A single counter per base name is not enough: a user can add `notes (2).txt` explicitly, and the next `notes.txt` clash must still get `(1)`. Probe the real set of names instead.

Hint 3

Pay attention to splitting the path `/` (it must give no components), to dotfiles whose only dot is at index 0, and to `list_dir` not creating nodes while it walks.

Hint 4

For the follow-up on huge numbers of duplicates, remember per folder and per (base, extension) the next tag worth trying, so each tag is skipped at most once.