KatInterview

Quota-Based Storage Service Simulator with Snapshots

Data Structure DesignAuto-graded · 23 test cases
MediumAuto-gradedData Structure Designdesignhash-mapsimulation

Last updated

Simulate a command stream on a storage service with per-user quotas, ownership transfer, prefix-based top-n listing, and per-user snapshot and restore.

Quota-Based Storage Service Simulator

You are given a log of commands for a small in-memory storage service. Process the commands in order and return one result per command.

The function receives two lists of equal length. commands[i] is the name of a command, and payloads[i] is the list of arguments for it. Return a list results where results[i] is the outcome of command i. Use None for a "no result" outcome.

Model

  • A file has a unique name (string) and a positive integer size. No two files can share a name, regardless of owner.
  • Every file has exactly one owner.
  • Every regular user has a remaining quota: the number of bytes they may still store. Adding a file reduces the owner's remaining quota by its size, and removing a file gives that size back to its current owner.
  • A special owner called "admin" exists from the start. Admin files are not subject to any quota, and admin has no quota to track. The "admin" identifier cannot be created, merged, backed up or restored, as described below.
  • Initially there are no files, no regular users and no backups.

Commands

  1. add_user, payload [user_id, quota]

    • If user_id is "admin" or a user with that id already exists, the result is False.
    • Otherwise create the user with remaining quota quota. The new user has no backup. The result is True.
  2. add_file, payload [name, size]

    • Adds a file owned by "admin".
    • If a file called name already exists, the result is False. Otherwise add it and the result is True.
  3. add_file_by, payload [user_id, name, size]

    • The result is None and nothing changes if any of these hold:
      • user_id is "admin",
      • user_id is not an existing user,
      • a file called name already exists,
      • size is greater than the user's remaining quota.
    • Otherwise the file is added with that user as owner, the user's remaining quota drops by size, and the result is the new remaining quota. (A file whose size equals the remaining quota is allowed.)
  4. get_file_size, payload [name]

    • The result is the file's size, or None if there is no such file.
  5. delete_file, payload [name]

    • If there is no such file, the result is None.
    • Otherwise remove it and the result is its size. If its owner is a regular user, that size is added back to the owner's remaining quota. Deleting an admin file refunds nothing.
  6. get_n_largest, payload [prefix, n]

    • Consider all files (of any owner) whose name starts with prefix. An empty prefix matches every file.
    • Order them by size descending, breaking ties by name ascending (plain string comparison).
    • The result is a list of at most n strings of the form "<name>(<size>)", taken from the front of that order.
    • If n <= 0 or nothing matches, the result is [].
  7. merge_user, payload [user_id_1, user_id_2]

    • The result is None and nothing changes if the two ids are equal, if either is "admin", or if either is not an existing user.
    • Otherwise:
      • every file owned by user_id_2 becomes owned by user_id_1,
      • user_id_2's remaining quota is added to user_id_1's remaining quota (file sizes themselves cause no change, since they were already counted),
      • user_id_2 is removed, together with any backup it had,
      • the result is user_id_1's new remaining quota.
  8. backup_user, payload [user_id]

    • If user_id is "admin" or not an existing user, the result is None.
    • Otherwise record a snapshot mapping each file name currently owned by the user to its size. This replaces any earlier snapshot of that user. The result is the number of files in the snapshot.
  9. restore_user, payload [user_id]

    • If user_id is "admin" or not an existing user, the result is None.
    • Otherwise do the following in order:
      1. Remove every file the user currently owns, refunding their sizes to the user's remaining quota. This happens even if the user has no snapshot.
      2. For each entry in the user's snapshot (if any), re-create the file for this user only if no file with that name exists at that moment. Each re-created file reduces the user's remaining quota by its size. No other quota check is made.
    • The result is the number of files re-created (0 if there is no snapshot). Restoring does not delete the snapshot.

You may assume every command name is one of the nine above and that payloads have the shape described.

Constraints

  • 1 <= len(commands) == len(payloads) <= 10^5
  • User ids and file names are strings; sizes and quotas are integers, with sizes positive and quotas non-negative.
  • n in get_n_largest is an integer and may be zero or negative.
  • Results must match the rules above exactly, including None versus False outcomes.

Examples

Example 1

Input: simulate_storage(['add_user', 'add_file_by', 'add_file_by', 'get_file_size', 'get_n_largest', 'delete_file'], [['alice', 100], ['alice', 'docs/a.txt', 30], ['alice', 'docs/b.txt', 80], ['docs/a.txt'], ['docs/', 5], ['docs/a.txt']])

Output: [True, 70, None, 30, ['docs/a.txt(30)'], 30]

Example 2

Input: simulate_storage(['add_user', 'add_user', 'add_file_by', 'add_file_by', 'merge_user', 'get_n_largest'], [['u1', 100], ['u2', 50], ['u1', 'a', 20], ['u2', 'b', 30], ['u1', 'u2'], ['', 10]])

Output: [True, True, 80, 20, 100, ['b(30)', 'a(20)']]

Example 3

Input: simulate_storage(['add_user', 'add_file_by', 'backup_user', 'delete_file', 'add_file', 'restore_user', 'get_n_largest'], [['u', 100], ['u', 'report', 40], ['u'], ['report'], ['report', 10], ['u'], ['', 10]])

Output: [True, 60, 1, 40, True, 0, ['report(10)']]

Hints

Hint 1

Keep one master table from file name to (size, owner), plus a per-user set of owned names so merge, backup and restore do not need to scan every file.

Hint 2

Route every ownership change through two small helpers (attach a file to a user, detach a file from its owner) so the quota arithmetic is written once. Remember that admin files never touch any quota.

Hint 3

For listing, filter by startswith and sort with the key (-size, name). Handle n <= 0 explicitly.

Hint 4

Restore always clears the user's current files first, even with no snapshot, and only then re-creates snapshot files whose names are free. A removed or newly created user must not carry over an old snapshot.