KatInterview

Path-Keyed File Store with Per-User Storage Quotas

Data Structure DesignAuto-graded · 20 test cases
MediumAuto-gradedData Structure Designhash-mapsimulationsorting

Last updated

Simulate a command stream on an in-memory file store with admin and user-owned files, prefix/suffix search, copying, and quota-driven eviction.

You are building a small in-memory file store and must replay a sequence of commands against it.

Files are identified by a full path string such as "/docs/a.txt". Paths are plain strings: there are no directories to track, and every path is unique across the whole store. Each file has an integer size and an owner. The owner is either the built-in admin or a registered user. The admin has no storage limit and is not a registered user.

Implement run_storage_commands(commands, params). The i-th command is commands[i] and its arguments are params[i]. Process the commands in order, starting from an empty store with no users, and return a list with one result per command.

Commands

CommandArgumentsBehaviorResult
addFile[name, size]Create an admin-owned file. Fails if name already exists.True if created, else False
copyFile[source, destination]Create destination with the same size and the same owner as source. Succeeds only if source exists and destination does not.True on success, else False
getFileSize[name]Look up a file's size.The size, or -1 if there is no such file
findFiles[prefix, suffix]Find every file whose name starts with prefix and ends with suffix. An empty prefix or suffix matches every name.A list of strings "<name>(<size>)", ordered by size descending, then by name ascending (plain string comparison)
addUser[user_id, capacity]Register a user with the given storage capacity. Fails if the user is already registered.True if registered, else False
addFileByUser[user_id, name, size]Create a file owned by user_id. It succeeds only if the user is registered, name is not already taken (by anyone), and used + size <= capacity, where used is the total size of the files that user currently owns.The user's remaining capacity (capacity - used) after the add, or -1 on failure
updateUserCapacity[user_id, new_capacity]Set the user's capacity to new_capacity. Then, while the user's used storage is greater than new_capacity, delete one of that user's files: the one with the largest size, and among equal sizes the one with the lexicographically largest name. Deleted names become free again.The number of files deleted, or -1 if the user is not registered

Rules to note

  • A copy of a user-owned file counts toward that user's used storage. copyFile does not check capacity: it always succeeds when the source exists and the destination is free, even if the owner ends up above capacity.
  • Copying a file onto its own name fails, because the destination already exists.
  • A file that brings usage exactly up to capacity is allowed.
  • Raising a capacity, or lowering it to a value that is still at least the current usage, deletes nothing and returns 0, but the new capacity is still stored.
  • Zero-size files are valid.
  • Admin-owned files never count against any user's capacity.
  • Files deleted by updateUserCapacity stop appearing in getFileSize and findFiles.

Constraints

  • len(commands) == len(params), and every command name is one of the seven listed above.
  • Paths and user IDs are non-empty strings, except that prefix and suffix in findFiles may be empty.
  • File sizes and capacities are non-negative integers.
  • Argument lists always have the arity shown in the table.
  • The expected result for each command is exactly as specified, so results must be returned as Python bool, int, or list[str] values as appropriate.

Examples

Example 1

Input: run_storage_commands(['addFile', 'addFile', 'getFileSize', 'copyFile', 'findFiles'], [['/docs/a.txt', 120], ['/docs/a.txt', 50], ['/docs/a.txt'], ['/docs/a.txt', '/backup/a.txt'], ['/', 'txt']])

Output: [True, False, 120, True, ['/backup/a.txt(120)', '/docs/a.txt(120)']]

Example 2

Input: run_storage_commands(['addUser', 'addFileByUser', 'addFileByUser', 'getFileSize', 'updateUserCapacity', 'findFiles'], [['u1', 100], ['u1', '/a', 40], ['u1', '/b', 60], ['/b'], ['u1', 50], ['/', '']])

Output: [True, 60, 0, 60, 1, ['/a(40)']]

Hints

Hint 1

Keep one dictionary from path to (size, owner) as the single source of truth for files, and one dictionary from user ID to its capacity and running used total.

Hint 2

Only user-owned files change a user's used total: update it on add and copy, and also when files are evicted. Do not apply any capacity check on copies.

Hint 3

For eviction, collect the user's files, sort them by size descending with ties broken by name descending, and delete from the front until used <= new capacity. Sorting by (size, name) with reverse=True does this in one step.

Hint 4

For findFiles, a linear scan with startswith and endswith followed by sorting on the key (-size, name) is enough.