KatInterview

File Storage with Quotas, Copy, and Compression

Data Structure DesignModel answer
MediumModel answerData Structure Designhash-mapheapobject-oriented-design

Last updated

Build an in-memory file system tracking per-user storage limits, supporting file operations including duplication, compression, and quota-driven eviction.

Problem

Implement an in-memory file storage system that tracks user quotas and supports file manipulation operations.

Storage Model

Files have three properties:

  • name: unique identifier (string)
  • size: storage consumed (positive integer)
  • owner: which user owns it

Users have two properties:

  • capacity: maximum storage allowed
  • used: current storage consumed

Constraint: For any user, used ≤ capacity must hold after every operation.

Operations to Implement

1. add_user(user_id, capacity) → bool

  • Register a new user with the given capacity and used = 0
  • Return false if the user already exists

2. add_file_by(user_id, name, size) → bool

  • Create a file owned by user_id
  • Fail if: user doesn't exist, name is taken, or adding would violate the capacity constraint

3. add_file(name, size) → bool

  • Create a file owned by a special admin user (has unlimited capacity)
  • The admin user should exist from initialization

4. copy_file(from_name, to_name) → bool

  • Duplicate an existing file with a new name
  • The copy inherits the original's size and owner
  • Fail if: source doesn't exist, destination name exists, or owner's capacity would be exceeded

5. get_file_size(name) → int | null

  • Return the file's size, or null if it doesn't exist

6. get_n_largest(prefix, suffix, n) → list[string]

  • Find all files whose names start with prefix AND end with suffix
  • Return up to n files formatted as "name(size)"
  • Sort by: size descending, then name ascending

7. update_capacity(user_id, new_capacity) → bool

  • Change a user's capacity
  • If used > new_capacity, automatically delete the user's largest files (by size, breaking ties by smallest name) until the constraint is satisfied
  • Return false if user doesn't exist

8. compress_file(name) → bool

  • Transform a file into compressed form
  • Only works if the file exists and is not already compressed
  • Compressed file: append ".compressed" to the name, set size to floor(original_size / 2)
  • Replace the original (remove old, add new)
  • Owner remains the same

9. decompress_file(name) → bool

  • Reverse compression
  • Only works if name ends with ".compressed"
  • Remove the suffix from the name, multiply size by 2
  • Fail if: the decompressed name already exists, or owner would exceed capacity
  • Replace the compressed file with the decompressed one

Implementation Notes

  • Choose clear conventions for edge cases (e.g., what happens with size-1 files, empty base names)
  • Focus on correct quota tracking across all operations
  • Aim for efficiency in finding and removing largest files (up to ~100,000 operations expected)

Hints

Hint 1

Track file metadata and user quotas in hash maps for O(1) lookups

Hint 2

Use a heap per user to efficiently find their largest files for eviction

Hint 3

Handle stale heap entries with lazy deletion using unique identifiers