KatInterview

Build a Bounded In-Memory File System with Auto-Rename

Data Structure DesignModel answer
MediumModel answerData Structure Designtriehash-mapfile-system

Last updated

Design a trie-backed file system supporting path-based file insertion and directory listing, with a per-directory capacity cap and OS-style duplicate renaming.

Bounded In-Memory File System

Build a class that simulates a hierarchical file system entirely in memory. You need to support two operations.

Operations

add_file(path: str) -> str

path is an absolute-style string such as "projects/backend/main.py". A leading / is optional and should be treated equivalently. The method must:

  • Create any intermediate directories that do not already exist.
  • Store a file node at the final component.
  • Return the actual path under which the file was stored (which may differ from the input if a rename occurred — see below).

list_dir(path: str) -> list[str]

Given a directory path (e.g. "projects/backend" or "" for the root), return the names of its immediate children in insertion order. A child may be a file or a subdirectory; return the name only, not the full path.

Constraints

1. Per-directory capacity limit

Each directory may hold at most 5 entries (files and subdirectories combined). If inserting a file would require adding a sixth entry to any directory along the path, reject the entire operation and leave the file system unchanged. This means no intermediate directories created during the call should persist either.

2. Automatic rename on collision

Within a single directory, every entry must have a unique name. If the requested leaf filename is already taken (by a file or a folder with the same name), do not reject the call — instead rename the new file using this scheme:

  • Split the name at its last . to get base and .ext. A name with no dot, or whose only dot is at the start (e.g. .gitignore), is treated as having no extension.
  • Append a numeric suffix to the base: base(1).ext, base(2).ext, and so on, picking the lowest integer that produces a free name.
  • Maintain a per-directory counter per requested name so you do not scan from 1 on every call.
  • Do not parse existing suffixes. Treat the incoming name as an opaque base. For example, if report(1).pdf already exists and a second report(1).pdf is added, the new file becomes report(1)(1).pdf.

Example sequence (all files land in the same directory):

add_file("docs/report.pdf")      → "docs/report.pdf"
add_file("docs/report.pdf")      → "docs/report(1).pdf"
add_file("docs/report(1).pdf")   → "docs/report(1)(1).pdf"

What to address

  1. Clarify any ambiguous behaviors before writing code (invalid paths, conflicts between files and directories, idempotency, case sensitivity).
  2. Implement both methods with correct handling of every constraint.
  3. Analyze time and space complexity.
  4. Call out the edge cases your implementation handles explicitly.

Hints

Hint 1

Model the file system as a trie where each node stores a dictionary of children. What extra per-node state do you need to enforce the capacity limit and to generate rename suffixes efficiently without scanning from 1 every time?

Hint 2

The capacity check only ever matters at one directory per call. Think about which directory that is — newly created directories start empty, so they can never be the one that overflows. Identify that single choke-point and check it before making any mutation to keep the operation atomic.

Hint 3

Extension splitting and suffix generation are their own mini-problems. Decide upfront: where is the extension boundary for names like `a.tar.gz` or `.bashrc`? And when must you increment past the stored counter — hint: a user could have added `report(1).pdf` manually before any collision occurred.