← Questions
Key-value store with constant-time set-all
0
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked in Nvidia's technical phone screen, last asked Apr 2026. It tests hash map, design and timestamps.
Implement a key-value store with three operations, all in O(1): set a key, get a key, and set every existing key to one value at once.
Part 1: set, get and set_all
Unless you agree on a different interface with the interviewer, assume:
-
set(key: str, value: int) -> Noneassigns a value to a key. -
get(key: str) -> int | Nonereturns the key's current value, orNoneif the key was never set. -
set_all(value: int) -> Nonemakes every key that exists right now returnvalue. Keys set afterwards are not affected.
set("a", 1)
set("b", 2)
set_all(9)
get("a") -> 9
set("b", 5)
get("b") -> 5 # set after set_all wins
get("a") -> 9
get("c") -> None # set_all does not create keys
set("c", 4)
get("c") -> 4
Part 2: a dataset store under a fixed disk capacity
A related task: store datasets by ID, each with a size, under a fixed total capacity.
-
put(dataset_id: str, size: int) -> strstores or replaces a dataset. Ifsizeexceeds the total capacity, fail and change nothing. Otherwise evict least recently used datasets until it fits. Return"ok"or an error. -
get(dataset_id: str) -> intreturns the size, or-1if absent. A successful get or put makes the dataset the most recently used.
capacity 10
put("A", 4) -> "ok"
put("B", 3) -> "ok"
get("A") -> 4
put("C", 5) -> "ok" # needs 5, only 3 free: evict B (least recently used)
get("B") -> -1
get("A") -> 4
get("C") -> 5
Constraints
- Up to 200,000 operations; keys are strings of 1 to 50 characters.
-
set_allmust not touch every stored key.
Clarifying questions
- Does
set_allaffect keys that do not exist yet? (No.) - Should the store be thread-safe?
- Part 2: when a dataset is replaced, is its old size freed before deciding what to evict?
- Part 2: what does
putreturn on failure, and does a failed put change the LRU order?
What a strong answer covers
- Part 1: a global counter; each
setstores(value, stamp);set_allstores its own(value, stamp).getreturns theset_allvalue when its stamp is newer than the key's. All O(1). - A clean class with the version logic hidden inside it, and why resetting the map on
set_allis not the same thing. - Part 2: a hash map plus a doubly linked list (or an ordered dict) for O(1) lookup and LRU order, with a running total of used space.
- Part 2 edge cases: a dataset larger than the capacity, replacing a dataset with a bigger or smaller size, evicting several datasets for one put.
- Tests that mix
set_allwith later sets, and evictions in the right order.
Follow-ups
- Add
delete(key)andcount()while keeping every operation O(1). - Part 2: some datasets are pinned and can never be evicted.
- Make either store safe for many threads without one global lock.
Where it's asked
- Nvidia · Technical phone screen · Last asked Apr 2026Live technical interview, 45 to 90 minutes; a project discussion, then a small task
What it tests
No official answer yet. Write yours on the right and run it.
Nothing published yet.
Sign in to submit your code; every attempt is kept here.
Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding
