KatInterview

Build a Multi-User File System with Quotas and Compression

Data Structure DesignAuto-graded · 16 test cases
MediumAuto-gradedData Structure Designhash-tablessimulationdesign

Last updated

Design an in-memory file manager tracking per-user storage limits, file operations, pattern matching, and size adjustments with atomic quota enforcement.

You are building an in-memory file management system that tracks files, enforces per-user storage limits, and supports file resizing operations. Implement a function that processes a sequence of operations on this system.

Your function receives two parallel lists: operations (operation names) and arguments (corresponding parameters for each operation). Process them in order and return a list of results.

Operations

set_user_quota — [user_id: str, quota: int]
Establishes a maximum storage limit of quota bytes for user_id. Returns False if the user currently occupies more than quota bytes (reject the change); otherwise applies the limit and returns True. Users without an explicit quota have unlimited storage.

add_file — [user_id: str, path: str, size: int]
Creates a file owned by user_id at path consuming size bytes. Returns False if path already exists or if adding this file would push user_id over their quota; returns True on success.

copy_file — [source_path: str, dest_path: str]
Duplicates the file at source_path to dest_path. The copy has the same owner and size as the original. Returns False if source_path does not exist, dest_path already exists, or the operation would exceed the owner's quota; returns True on success.

search_files — [prefix: str, suffix: str]
Returns a sorted list (lexicographically ascending) of all file paths that begin with prefix and end with suffix.

compress_file — [path: str, factor: float]
Adjusts the file at path to a new size equal to floor(current_size × factor) bytes. The owner's storage usage updates accordingly. If factor > 1.0 (expansion) and the increase would exceed the owner's quota, the operation must fail atomically: return False and leave both file size and usage unchanged. Returns False if path does not exist; returns True on successful resize.

Constraints

  • 1 ≤ len(user_id), len(path) ≤ 100
  • 1 ≤ size, quota ≤ 10^9
  • 0.0 < factor ≤ 10.0
  • At most 5 × 10^4 total operations

Examples

Example 1

Input: process_file_operations(['set_user_quota', 'add_file', 'add_file', 'copy_file', 'compress_file', 'copy_file', 'search_files', 'compress_file'], [['u1', 100], ['u1', '/docs/notes.txt', 60], ['u1', '/docs/code.py', 50], ['/docs/notes.txt', '/backup/notes.txt'], ['/docs/notes.txt', 0.5], ['/docs/notes.txt', '/backup/notes.txt'], ['/', '.txt'], ['/docs/notes.txt', 3]])

Output: [True, True, False, False, True, True, ['/backup/notes.txt', '/docs/notes.txt'], False]

Hints

Hint 1

Track two mappings: one from paths to (owner, size) pairs, and one from user IDs to current usage and quota.

Hint 2

Before any mutation that increases a user's storage, validate that the new total stays within their quota. Only commit the change after validation passes.

Hint 3

For compress_file, compute the size delta first. If it's positive (expansion), check the quota; if negative or zero (compression), the operation always succeeds from a quota perspective.