Skip to content

visual walkthrough

Time Based Key-Value Store

MediumUpper Bound on TimestampsReported at: AmazonMicrosoftGoogle+2

A design question whose core is upper-bound search.

Solve on LeetCode

The idea

Timestamps only go up, so appending each set keeps the history sorted with no extra work.

get(t) is then "the last entry with time ≤ t": a binary search for a boundary.

Complexity

approachtimespace
Scan the historyO(n) per getO(n)
Binary search the historyO(log n) per getO(n)

More walkthroughs