KatInterview

Build an In-Memory File Store with Quotas and Backups

Data Structure DesignModel answer
MediumModel answerData Structure Designdata-structure-designhash-mapheap

Last updated

Implement an in-memory file store with prefix top-N queries, per-user quotas, user merging, and per-user backup and restore that never disturb other users.

Task

Implement a class that simulates a simple in-memory file store. The requirements arrive in stages, and every stage must keep the earlier ones working. Explain your data model, the invariants you maintain, and the complexity of each operation, then write the code.

Stage 1: Basic files

  • add_file(name, size) stores a file with a globally unique name.
  • get_file_size(name) returns the size of a stored file.
  • delete_file(name) removes a file and reports what was removed.

Stage 2: Prefix ranking

  • get_n_largest(prefix, n) returns the n biggest files whose names start with prefix.

Stage 3: Users and quotas

  • add_user(user_id, capacity) creates a user with a storage limit.
  • add_file_by(user_id, name, size) stores a file on behalf of that user, but only if it fits inside the user's remaining capacity.
  • Files added through Stage 1 belong to a built-in user with unlimited capacity.

Stage 4: Merging users

  • merge_user(user_id1, user_id2) moves all files and the total capacity of the second user into the first, and then removes the second user.

Stage 5: Backup and restore

  • backup_user(user_id) saves a snapshot of that user's current files.
  • restore_user(user_id) returns that user's files to the snapshot state.
  • Backups and restores must never change another user's files or any user's capacity.

What to specify

Since the specification leaves details open, state your return-value conventions explicitly. A reasonable set:

  • Operations that fail (duplicate name, unknown user, not enough room) return a failure value such as False or None.
  • get_n_largest returns strings of the form name(size), ordered by size descending and then by name ascending.
  • add_file_by returns the user's remaining capacity on success.
  • merge_user returns the remaining capacity of the surviving user, and fails if the two ids are equal or either is unknown.
  • backup_user and restore_user return the number of files saved or restored.

Questions to address

  1. Which data structures give O(1) existence checks and O(1) capacity checks?
  2. How do you guarantee that merge and restore cannot corrupt usage accounting?
  3. What should restore do when a file name in the snapshot has since been taken by someone else?
  4. How would you speed up prefix queries if they dominated the workload?

Hints

Hint 1

Keep one global map from file name to (owner, size), since names are unique across all users, and give each user a counter of used space that you update incrementally.

Hint 2

Funnel every mutation through two small helpers (one to insert, one to remove) that update the global map, the owner's file set, and the owner's used counter together; merge and restore can then be built from them.

Hint 3

A snapshot must be an independent copy of name-to-size. On restore, remove only the current user's files, then re-add snapshot files whose names are still free, and leave capacity untouched.