← 返回 netflix 的题目列表Thread-Safe KV Store / Atomic Counter / Blocking Queue
类型:qbank
Netflix Infra and Data Platform rounds include hands-on concurrency: synchronized KV store, atomic counter, blocking queue, double-checked locking, and TTL map variants.
Requirements
KV variant: implement get, put, and optionally delete with thread safety.
TTL variant: entries expire after a fixed duration; support lazy cleanup and capacity cleanup.
Atomic counter variant: increment(), decrement(), get(), and optionally wait until value reaches zero.
Atomic counter should also expose add(delta) -> int returning the updated value, with increment/decrement delegating to it. In Python the GIL is not sufficient: x += 1 is a non-atomic load-add-store, so the read-modify-write must run under the same lock or one update is lost. Common follow-ups: a compare_and_set(expected, new) CAS API, reducing contention via striped / per-thread counters, and a lock-free variant using hardware atomics.
Blocking queue variant: offer(), poll(), peek(), size(), and wait / signal behavior.
Explain validation and production behavior, not just code.
Notes
Java can use ConcurrentHashMap, AtomicInteger, ReentrantLock, and Condition. Python can use threading.Lock / RLock / Condition with careful critical sections.
Start with correctness under a coarse lock. Then discuss fine-grained lock striping if contention is high.
Double-checked locking is usually about avoiding repeated expensive initialization while still publishing a fully initialized object safely.
For TTL maps, get can check expiry for the requested key while background cleanup removes old keys globally.
Blocking queue correctness depends on condition loops, not one-time if: always re-check the predicate after waking.
Count-down-latch variant (__init__(count), count_down(), wait(), plus a count_up() twist that makes it re-armable): back it with a Condition; count_down decrements and notify_all() exactly when the count hits zero; wait() blocks in a while count > 0: loop (the loop, not an if, is what guards against spurious wakeups). notify_all over notify because every waiter must proceed once the gate opens.
Preparation
Implement a coarse-lock KV store and a striped-lock KV store.
Implement a blocking queue with not_empty and not_full conditions.
Implement an atomic counter with waitUntilZero().
Be ready to explain race cases: lost update, stale read, deadlock, missed signal, and cleanup racing with get.