Katkat

Extend cloud file system with copy and compression

MetaSoftware Engineer · Online Assessment

Last updated

This question evaluates understanding of data structures and algorithms for stateful resource management, covering bookkeeping of per-user quotas, file metadata operations (copy, compress/decompress), string filtering and sorting, and efficient selection and deletion of largest elements.

In-Memory Cloud File System V2: Copy, Capacity Updates, Compress/Decompress

Design an in-memory file system with per-user quotas and additional operations.

Data model / rules

  • Each file has:
  • name (unique string)
  • size (positive int)
  • owner (user id)
  • Each user has:
  • capacity
  • used
  • A user may only own files such that used <= capacity.

API

  1. add_user(user_id, capacity) -> bool
  • Create a user with used = 0.
  1. add_file_by(user_id, name, size) -> bool
  • Add a new file owned by user_id.
  • Fail if user missing, name exists, or would exceed capacity.
  1. add_file(name, size) -> bool
  • Add a file owned by a special admin user with effectively unlimited capacity (or explicitly create admin with very large capacity).
  1. copy_file(from_name, to_name) -> bool
  • Duplicate an existing file to a new name.
  • The copy has the same size and owner as the source.
  • Fail if from_name missing, to_name already exists, or the owner’s capacity would be exceeded.
  1. get_file_size(name) -> int | null
  • Return size or null if missing.
  1. get_n_largest(prefix, suffix, n) -> list<string>
  • Filter files with name starting with prefix and ending with suffix.
  • Return up to n formatted: "name(size)".
  • Sort by size desc, then name asc.
  1. update_capacity(user_id, new_capacity) -> bool
  • Update the user’s capacity.
  • If used > new_capacity, repeatedly delete the user’s largest files (tie-break by name asc/desc—state your tie-break clearly) until used <= new_capacity.
  • Return false if user missing.
  1. compress_file(name) -> bool
  • Convert a file into a compressed version:
  • Only if name exists and does not already represent a compressed file.
  • New file name becomes name + "compress" (or another fixed suffix; be consistent).
  • New size becomes floor(old_size / 2).
  • Ownership stays the same.
  • Replace the original (remove old name, add new name).
  • Update the owner’s used accordingly.
  1. decompress_file(name) -> bool
  • Only valid if name ends with the compression suffix.
  • New name removes the suffix; new size becomes old_size * 2.
  • Fail if the decompressed name already exists or if the owner would exceed capacity.
  • Replace the compressed file with the decompressed one.

Notes

  • Aim for correct bookkeeping of used and efficient deletion of largest files (e.g., heap + lazy deletion).
  • Assume up to ~1e5 operations.