Implement cloud storage with quotas and compression
Company: Coinbase
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Quick Answer: This question evaluates the ability to design and implement in-memory state management and algorithmic logic for user-scoped storage, including capacity tracking, deterministic eviction policies, file metadata manipulation, and integer/string operations.
Constraints
- 0 <= len(queries) <= 10^5
- 0 <= capacity, newCapacity <= 2^31 - 1
- 1 <= size <= 2^31 - 1 for ADD_FILE operations
- userId and fileName are non-empty strings
- File names are unique per user
- Compressed sizes use integer division, so a compressed file may become size 0
Examples
Input: ([['ADD_USER', 'alice', '10'], ['ADD_FILE', 'alice', 'a.txt', '6'], ['GET_FILE_SIZE', 'alice', 'a.txt'], ['COMPRESS_FILE', 'alice', 'a.txt'], ['GET_FILE_SIZE', 'alice', 'a.txt'], ['GET_FILE_SIZE', 'alice', 'a.txt.compressed'], ['DECOMPRESS_FILE', 'alice', 'a.txt.compressed'], ['GET_FILE_SIZE', 'alice', 'a.txt'], ['UPDATE_CAPACITY', 'alice', '6']],)
Expected Output: ['true', 'true', '6', 'true', '', '3', 'true', '6', '0']
Explanation: Basic flow: add a user, add a file, compress it, verify the old name disappears and the new name exists with half size, then decompress it back and update capacity without needing deletion.
Input: ([['ADD_USER', 'bob', '20'], ['ADD_FILE', 'bob', 'alpha', '5'], ['ADD_FILE', 'bob', 'beta', '7'], ['ADD_FILE', 'bob', 'gamma', '7'], ['UPDATE_CAPACITY', 'bob', '10'], ['ADD_FILE', 'bob', 'delta', '5'], ['UPDATE_CAPACITY', 'bob', '5'], ['GET_FILE_SIZE', 'bob', 'alpha'], ['GET_FILE_SIZE', 'bob', 'delta']],)
Expected Output: ['true', 'true', 'true', 'true', '2', 'true', '1', '5', '']
Explanation: When capacity drops to 10, the two 7-byte files must be removed; between beta and gamma, gamma is deleted first because it is lexicographically larger. Later alpha and delta tie at size 5, so delta is deleted.
Hints
- Use a hash map for each user's files so existence checks and size lookups are O(1) on average.
- For UPDATE_CAPACITY, a priority queue can give the largest file quickly. Since files may be renamed or deleted, think about lazy deletion of outdated heap entries.