← Questions

Key-value store with constant-time set-all

0
NvidiaCodingLast asked Apr 2026

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) -> None assigns a value to a key.
  • get(key: str) -> int | None returns the key's current value, or None if the key was never set.
  • set_all(value: int) -> None makes every key that exists right now return value. 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) -> str stores or replaces a dataset. If size exceeds 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) -> int returns the size, or -1 if 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_all must not touch every stored key.

Clarifying questions

  • Does set_all affect 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 put return on failure, and does a failed put change the LRU order?

What a strong answer covers

  • Part 1: a global counter; each set stores (value, stamp); set_all stores its own (value, stamp). get returns the set_all value 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_all is 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_all with later sets, and evictions in the right order.

Follow-ups

  • Add delete(key) and count() 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

What it tests

hash map · design · timestamps

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