32A configuration store that remembers every change
32 A configuration store that remembers every change
Time Based Key-Value Store, LeetCode 981, is on the LeetCode company tag lists for OpenAI (1 source) and Netflix, Netflix's six-month list included (2 sources), and a prep site's guide to OpenAI's software engineer loop names a time-based key-value store (1 source). Snapshot Array, LeetCode 1146, is the same idea with snapshot numbers in place of times. It's on OpenAI's tag list (1 source) and in the same guide (1 source). Two OpenAI software engineer candidates report a coding round in parts on a social graph with snapshots, starting from follow, unfollow and snapshot operations (2 reports). This is practice in that shape, set in one system, not a leaked question or LeetCode's version.
Set a 40-minute timer. Read one level at a time, and run only that level's tests until they pass:
pytest problems/32_versioned_store -k level1. Earlier levels must keep passing.
The store. A service reads its settings from a configuration store, and every change to a setting carries the
time it took effect, in seconds (an int or a float). When something breaks, the on-call engineer asks what a
setting was at a given moment. Keys are strings. Values are any Python value except None: numbers, strings,
dicts. Values don't have to support <.
Level 1. Write ConfigStore() with two methods.
set(key, value, time)records that fromtimeon,keyholdsvalue, until its next change.get(key, time)returns the value of the latest change tokeyat or beforetime. When the key has no change at or beforetime, or was never set, it returnsNone.
At this level, each key's times never go down from one set to the next. Two changes to one key at the same time
can happen, and the one made by the later call wins.
store = ConfigStore()
store.set("timeout", 30, 100)
store.set("timeout", 45, 160)
store.get("timeout", 99) -> None
store.get("timeout", 100) -> 30
store.get("timeout", 159.5) -> 30
store.get("timeout", 160) -> 45
store.get("timeout", 10**9) -> 45
A key can collect a long history, so get must be O(log n) for a key with n changes. One test reads a key with
50,000 changes using times that count every comparison made with them, and fails past 40 comparisons per read.
Level 2. What a real store needs next.
- Changes can arrive late. A
setmay now carry a time earlier than the key's latest change, from a replica that caught up, say. It takes its place in time:getsees it from its time until the next change after it. At a time where the key already has a change, the later call still wins. delete(key, time)records that fromtimeon,keyhas no value, until its nextset.getreturnsNonein between. The history beforetimestays:getat an earlier time still answers. A delete is a change like any other, so it can arrive late too, and it ties with other changes the same way.changes(key, start, end)returns how many changes tokey, sets and deletes, havestart <= time < end. Every call counts, including one that a later call at the same time overrides. Whenend <= startthe range is empty and the count is 0, and a key that was never changed has 0.
A late change may cost O(n) to slot into place. get and changes stay O(log n). One test calls changes on a
key with 50,000 changes, with times that count their comparisons, and fails past 80 comparisons per call.
Level 3. Snapshots.
snapshot()saves the store as it stands and returns the snapshot's number: 0 for the first call, then 1, 2, and so on.get_snapshot(key, snap)returns the valuekeyheld when snapshotsnapwas taken. A key holds the value of its latest change by time, so a late change that lands before the key's latest one doesn't change what it holds. When that latest change is a delete, or the key had no change yet, the answer isNone. Asnapthat no call tosnapshot()has returned raisesValueError.
Taking a snapshot must not copy the store. One test takes 500 snapshots of a store with 500 keys and fails if that holds 256 KiB or more at once.
Things to get right: a read at exactly the time of a change, a read before the first change, two changes at the same time, values that can't be compared, a delete that the past still remembers, which end of a range is included, and a key that was first set after a snapshot.
Be ready for what comes next: a history too big for one machine's memory, many readers and writers at once, keeping only the last few days of history, and reading the whole store as of a time rather than one key.