Implement a disk space manager with eviction
Company: NVIDIA
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Quick Answer: This question evaluates understanding of in-memory resource management, capacity accounting, eviction policies, and data-structure design for efficient dataset lookup and size tracking in the Coding & Algorithms domain.
Constraints
- 0 <= total_capacity <= 10^9
- 0 <= len(operations) <= 2 * 10^5
- For each put operation, 1 <= size <= 10^9
- datasetId is a non-empty string
- Use LRU eviction: successful get and successful put/update make the dataset most recently used
Examples
Input: (5, [])
Expected Output: []
Explanation: No operations means no output.
Input: (10, [('put', 'A', 4), ('put', 'B', 3), ('get', 'A'), ('put', 'C', 5), ('get', 'B'), ('get', 'A'), ('get', 'C')])
Expected Output: ['ok', 'ok', 4, 'ok', -1, 4, 5]
Explanation: After get('A'), A becomes most recent. Putting C needs 5 units, so B is evicted first as the least recently used dataset.
Input: (8, [('put', 'X', 5), ('put', 'Y', 2), ('put', 'X', 7), ('get', 'Y'), ('get', 'X'), ('put', 'Z', 9), ('get', 'X')])
Expected Output: ['ok', 'ok', 'ok', -1, 7, 'error', 7]
Explanation: Updating X from 5 to 7 frees its old size first, then Y is evicted to make space. Putting Z fails because 9 is larger than total capacity 8, so the state stays unchanged.
Input: (6, [('put', 'A', 2), ('put', 'B', 2), ('put', 'C', 2), ('get', 'A'), ('put', 'D', 3), ('get', 'B'), ('get', 'C'), ('get', 'A'), ('get', 'D')])
Expected Output: ['ok', 'ok', 'ok', 2, 'ok', -1, -1, 2, 3]
Explanation: After get('A'), the LRU order is B, C, A. To store D with size 3, B and then C are evicted.
Input: (0, [('get', 'A'), ('put', 'A', 1), ('get', 'A')])
Expected Output: [-1, 'error', -1]
Explanation: With zero capacity, no positive-size dataset can be stored.
Hints
- You need two things at once: fast lookup by datasetId and fast access to the least recently used dataset.
- When updating an existing dataset, free its old size before deciding whether you still need to evict other datasets.