Design an in-memory cloud storage system
CloudKitchensSoftware Engineer · Technical ScreenLast updated
This problem tests your ability to design an efficient in-memory storage manager that handles concurrent file operations, user-based quotas, and data eviction policies.
Problem Statement
Design an in-memory cloud storage system that manages users, files, and storage quotas. Your system should support the following operations:
create_user(user_id, capacity): Initializes a user with a maximum storage capacity (in bytes).upload_file(user_id, file_id, size): Adds a file to a user's account. If the file exceeds the remaining capacity, the operation must fail. If the user does not exist, throw an error.delete_file(user_id, file_id): Removes a specific file and frees up space.update_quota(user_id, new_capacity): Sets a new capacity limit. If the new capacity is lower than the current total file size, the system must automatically delete the user's largest files until they are within the new limit. If multiple files have the same size, pick the one with the smallestfile_id(lexicographically) to delete.
Constraints
user_idandfile_idare strings.sizeis a positive integer.- Total operations may reach 10^5.
update_quotaoperations should be efficient even when a large number of files need to be evicted.
Example
# Setup
system = CloudStorage()
system.create_user("alice", 100)
system.upload_file("alice", "a.txt", 60)
system.upload_file("alice", "b.txt", 50) # Fails: 60+50 > 100
system.update_quota("alice", 40)
# System deletes "a.txt" (60 bytes) to fit quota.
# Remaining storage: 0/40. "b.txt" was never added.
Edge Cases
- Updating quota to zero.
- Deleting non-existent files.
- Attempting to upload a file larger than the entire quota.
Hints
Hint 1
Maintain a map of user_id to a data structure that tracks their files and current total usage.
Hint 2
For efficient eviction during a quota reduction, store user files in a sorted structure or use a min/max-heap to quickly identify the largest files.
Hint 3
Ensure that file deletions and quota updates are atomic to avoid inconsistent states during concurrent execution.
Follow-up questions
- How would you modify this system to support shared folders where multiple users can contribute to a single directory's quota?
- What concurrency primitives would you use if this system were accessed by multiple threads simultaneously?
- If the file metadata needed to be persisted to a disk, how would you handle recovery after a system crash?
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.