KatInterview

Metadata Store for Folders, Quotas and Largest-File Queries

System DesignModel answer
HardModel answerSystem Designsystem-designtreesorted-index

Last updated

Design an in-memory hierarchical file metadata service with create, recursive delete, fast top-k largest files per directory, and nested quota enforcement, plus a multi-node scaling plan.

Metadata service for a hierarchical file store

Design a service that keeps the metadata of a file system (folders and files) entirely in memory. It must answer queries with low latency, and the query that finds the biggest files under a folder must be especially fast, even if that makes writes more expensive.

Operations

  1. create(path, size) -> bool: add a file at path with the given size. Missing intermediate folders are created automatically. Return false if the path already exists, if a component of the prefix is a file, or if the new file would push any ancestor folder over its quota.
  2. delete(path) -> bool: remove a file, or a folder together with everything below it. Return false if the path does not exist.
  3. get_top_k(path, k) -> list[str]: return the paths of the k largest files in the folder's whole subtree. Order by size descending, and break ties by path ascending so results are deterministic.
  4. update_quota(path, limit) -> bool: set a storage cap on a folder. Return false if the folder's current usage is already above limit, or if limit is larger than the quota of its nearest ancestor that has one.

Scale and assumptions

  • Up to about 10^6 files and folders in total.
  • Everything fits in RAM on one machine for the core design.
  • Quotas nest: a folder's usage can never exceed the quota of any ancestor.

Example

create("/home/user/data.txt", 100)
create("/home/user/logs/app.log", 200)
get_top_k("/home/user", 1)  ->  ["/home/user/logs/app.log"]

What to deliver

  • Requirements and scale estimates, including memory.
  • The data model and the algorithm for each operation, with complexity.
  • Code for the core structure.
  • Trade-offs against alternative designs, and how you would extend this to several machines.

Hints

Hint 1

If every folder could hand you its files already sorted by size, what would get_top_k reduce to? Think about what you would have to maintain on writes to make that true.

Hint 2

Keep a running byte total per folder so a quota check is O(1) per ancestor. Validate every ancestor before changing anything, so a rejected create leaves no partial state.

Hint 3

To shard across machines, partition by top-level path prefix. Then ask what breaks for queries and quotas that span partitions, and how a k-way merge of per-shard top-k lists helps.