Coding · must-tier · asked at Amazon, Apple, Google, Meta, Microsoft, Netflix
Time Based Key-Value Store
Problem
Design a key-value store where every write is tagged with a timestamp and reads can ask for the value "as of" a given time. Implement a class TimeMap with:
set(key, value, timestamp): record thatkeyhadvalueat timetimestamp.get(key, timestamp): return the value that was set forkeywith the largest timestamp<= timestamp. If no such write exists, return the empty string.
For any given key, calls to set arrive with strictly increasing timestamps.
- Keys and values are non-empty strings of at most 200 characters.
- Timestamps are positive integers no larger than 1,000,000,000.
- Up to 500,000 calls to
setandgetcombined, so per-call work must be logarithmic or better.
Examples
set("region", "eu-west", 5), thenget("region", 5)→"eu-west"andget("region", 9)→"eu-west"(the write at time 5 is the latest one at or before 9).- Continuing,
set("region", "us-east", 12), thenget("region", 12)→"us-east",get("region", 30)→"us-east", andget("region", 11)→"eu-west". get("region", 4)→"", because nothing was written at or before time 4.get("tier", 100)→""as well, since that key was never set.
Hints
- Group the writes by key. Within one key, what order do the timestamps arrive in?
getasks for the last timestamp that is not greater than a query value in a sorted list. Which search answers "rightmost element<=x"?- Hash map from key to a list of
(timestamp, value)pairs;getusesbisect_righton the timestamps and takes the element just before the insertion point.
Last reviewed 8 September 2026
Locked
Approach · Solution (Python) · Complexity · Follow-ups · Company variations · Tests
The rest of this question — the worked approach, the Python solution with passing tests, complexity, follow-ups and how each company frames it — is part of the guides.
Get the full guide
Pick the company you're interviewing with. Each guide has every question tiered for that loop — one-time purchase, lifetime access.
Secure payment by Stripe · instant access · refund questions: [email protected]
Also in: Apple, Google, Meta, Microsoft, Netflix
Interviewing at more than one? All 6 guides for $499 · save $395
Not ready to buy? Get the free starter pack — 13 questions with full solutions.