← 返回 nvidia 的题目列表Disk Space Manager / KV Store with setAll
类型:qbank
Implement storage-like APIs: a disk-space manager with `put(datasetId, size)` and `get(id)` under capacity / eviction constraints, or a KV store supporting `set`, `get`, and O(1) `setAll`.
Requirements
Disk Space Manager
Initialize with total capacity. Implement:
class DiskSpaceManager {
DiskSpaceManager(long capacity);
boolean put(String datasetId, long size);
Long get(String datasetId);
}
Behavior:
put(id, size) stores or replaces a dataset.
If there is not enough free capacity, the manager may evict other datasets until enough space exists.
The eviction policy is open unless specified; LRU is a reasonable choice if allowed.
If the requested dataset itself is larger than total capacity, return error / false.
get(id) returns the stored size or null / -1 if missing.
KV Store with setAll
Implement:
set(key, value)
get(key) -> value
setAll(value)
setAll should update the logical value of every key without iterating through all keys.
Notes
Disk Space Manager is a cache / resource-manager design in miniature. Track:
used_capacity.
id -> size.
An eviction structure such as LinkedHashMap for LRU.
On update, subtract the old size before checking capacity. For LRU, get should refresh recency if the interviewer expects cache semantics.
setAll is solved with timestamps:
global_value, global_time
per_key[key] = (value, time)
get(key): return per-key value if key_time > global_time else global_value
Clarify whether get on a never-set key after setAll should return the global value or missing; both variants are common.
Preparation
Implement LRU with LinkedHashMap in Java and OrderedDict in Python.
Practice the timestamp trick for setAll until it is automatic.
Ask whether replacement of an existing dataset counts as a new access and whether eviction may evict the just-updated key.