In-Memory Cloud Storage: Design and Implementation
Design and implement an in-memory cloud storage system that maps file names to their metadata (specifically, size) and supports both single-user and multi-user operations.
Assumptions and Constraints
- File names must be unique per user. Single-user functions operate within a special default user namespace.
- File sizes are non-negative integers, representing bytes.
- Each user has a
capacity, which is the maximum total size (sum of all file sizes) allowed for that user. - The
get_n_largest(prefix, n)operation should return thenlargest files across all users whose names start with the givenprefix. Each result entry must include theuser_id,name, andsize. - The
merge_user(u1, u2)operation combinesuser2's data intouser1.user1persists,user2is deleted. The newuser1's capacity becomes the sum ofuser1's anduser2's original capacities. - During a
merge_useroperation, if file name collisions occur (a file inuser2has the same name as a file inuser1), the incoming conflicting file(s) fromuser2must be renamed by appending a suffix to ensure uniqueness (e.g., "name (merged 2)"). backup_userandrestore_useroperations are performed per user and do not affect other users' files or capacities.
Required API
Single-user (default namespace):
add_file(name, size)get_file_size(name)→sizeorNonedelete_file(name)get_n_largest(prefix, n)→listof(user_id, name, size)
Multi-user:
add_user(user_id, capacity)add_file_by(user_id, name, size): Enforce per-user capacity limits.merge_user(user_id1, user_id2): Combines users and their files; capacity sums; rename on conflicts.backup_user(user_id)→version_id: Creates versioned backups per user.restore_user(user_id, version_id=None): Restores a user to a saved version (latest ifversion_idisNone).
Deliverables
- Detailed data structures and algorithms to support all specified operations.
- Clear semantics for edge cases, including capacity enforcement, file name collisions during merge, and restore behavior.
- A discussion of time and space complexity, with potential optimizations, especially for prefix queries.
- A working in-memory implementation in any mainstream language, accompanied by small usage examples and tests.
AI interview prep
Core dilemma
Efficiently managing a multi-tenant, in-memory data store with complex cross-user queries (prefix-based, largest N) and atomic user-level operations (merge, versioned backup/restore), while ensuring thread safety, consistency, and optimal memory footprint.
3 tradeoffs and 3 stress tests locked
Interview prep
Unlock the interviewer X-Ray rubric, tradeoff matrix, stress tests, clarifying questions, and key concepts.
In-Memory Cloud Storage: Design and Implementation
Design and implement an in-memory cloud storage system that maps file names to their metadata (specifically, size) and supports both single-user and multi-user operations.
Assumptions and Constraints
- File names must be unique per user. Single-user functions operate within a special default user namespace.
- File sizes are non-negative integers, representing bytes.
- Each user has a
capacity, which is the maximum total size (sum of all file sizes) allowed for that user. - The
get_n_largest(prefix, n)operation should return thenlargest files across all users whose names start with the givenprefix. Each result entry must include theuser_id,name, andsize. - The
merge_user(u1, u2)operation combinesuser2's data intouser1.user1persists,user2is deleted. The newuser1's capacity becomes the sum ofuser1's anduser2's original capacities. - During a
merge_useroperation, if file name collisions occur (a file inuser2has the same name as a file inuser1), the incoming conflicting file(s) fromuser2must be renamed by appending a suffix to ensure uniqueness (e.g., "name (merged 2)"). backup_userandrestore_useroperations are performed per user and do not affect other users' files or capacities.
Required API
Single-user (default namespace):
add_file(name, size)get_file_size(name)→sizeorNonedelete_file(name)get_n_largest(prefix, n)→listof(user_id, name, size)
Multi-user:
add_user(user_id, capacity)add_file_by(user_id, name, size): Enforce per-user capacity limits.merge_user(user_id1, user_id2): Combines users and their files; capacity sums; rename on conflicts.backup_user(user_id)→version_id: Creates versioned backups per user.restore_user(user_id, version_id=None): Restores a user to a saved version (latest ifversion_idisNone).
Deliverables
- Detailed data structures and algorithms to support all specified operations.
- Clear semantics for edge cases, including capacity enforcement, file name collisions during merge, and restore behavior.
- A discussion of time and space complexity, with potential optimizations, especially for prefix queries.
- A working in-memory implementation in any mainstream language, accompanied by small usage examples and tests.