KatInterview

Multi-User File System with Snapshots and Account Consolidation

Data Structure DesignAuto-graded · 13 test cases
EasyAuto-gradedData Structure Designhash-mapsimulationdata-structure-design

Last updated

Build an in-memory file storage system with per-user quotas, global file uniqueness, ranked retrieval, account merging, and atomic snapshot restore.

Build an in-memory file storage system that tracks users, enforces storage quotas, and supports snapshots with atomic restore operations.

Implement a function file_system_operations(operations: List[str], arguments: List[List]) -> List that processes a sequence of operations and returns their results.

The system supports these operations:

add_user: ["add_user", [user_id, capacity]]
Creates a user account with the specified storage capacity in bytes. Returns True if successful. Returns False if user_id already exists or capacity < 1.

add_file: ["add_file", [user_id, file_name, file_size]]
Adds a file to the user's storage. File names must be globally unique across all users. Returns True if successful. Returns False if:

  • user_id does not exist
  • file_size < 1
  • file_name already exists anywhere in the system
  • Adding the file would exceed the user's capacity (current usage + file_size > capacity)

get_largest_files: ["get_largest_files", [user_id, k]]
Returns up to k file names owned by user_id, ordered by size (largest first). Ties are broken alphabetically in ascending order. Returns an empty list if user_id does not exist or has no files.

merge_accounts: ["merge_accounts", [target_user_id, source_user_id]]
Consolidates two accounts:

  • All files from source_user_id are transferred to target_user_id
  • target_user_id's capacity increases by source_user_id's capacity
  • source_user_id and its snapshot (if any) are permanently deleted
  • Returns False if target_user_id == source_user_id or either user does not exist
  • Returns True on success

backup_user: ["backup_user", [user_id]]
Creates a snapshot of all files currently owned by user_id. Overwrites any previous snapshot for this user. Returns False if user_id does not exist, otherwise True.

restore_user: ["restore_user", [user_id]]
Restores files from the user's most recent snapshot. This operation is atomic (all-or-nothing):

  • Returns False if user_id does not exist or has no snapshot
  • Returns False if any file in the snapshot is currently owned by a different user
  • Returns False if the snapshot's total size exceeds the user's current capacity
  • If all checks pass: deletes all current files owned by user_id, restores all snapshot files, and returns True
  • If any check fails: no changes are made to the system

Constraints

  • user_id and file_name are non-empty alphanumeric strings
  • 1 <= capacity, file_size <= 10^9
  • 1 <= k <= 10^3
  • Total operations <= 5 × 10^4

Examples

Example 1

Input: file_system_operations(['add_user', 'add_user', 'add_file', 'add_file', 'add_file', 'backup_user', 'add_file', 'get_largest_files', 'restore_user', 'get_largest_files'], [['alice', 100], ['bob', 50], ['alice', 'a.txt', 40], ['alice', 'b.txt', 30], ['alice', 'c.txt', 40], ['alice'], ['alice', 'd.txt', 20], ['alice', 2], ['alice'], ['alice', 5]])

Output: [True, True, True, True, False, True, True, ['a.txt', 'b.txt'], True, ['a.txt', 'b.txt']]

Hints

Hint 1

Use three hash maps: one for user data (capacity and files), one for global file ownership, and one for snapshots.

Hint 2

For restore_user, validate all preconditions before making any changes. Only mutate state after confirming the operation is valid.

Hint 3

When merging accounts, remember to update the global ownership map and delete the source user's snapshot.