Skip to content
BytePatterns

Value at a Point in Time

MediumSearching#binary-search#upper-bound#versioned-store~25m

Problem

A config service keeps every version of every setting. set(key, value, t) records that key took value at time t, and for any one key the times always arrive in strictly increasing order. get(key, t) returns the value the key had at time t, meaning the value from the latest set with time at most t, or "" if the key had no value yet. There are up to 200,000 calls in total and times go up to 10^7, so a get must not scan all of a key's versions.

Examples

Input:  set("theme", "light", 1), get("theme", 1), get("theme", 3),
        set("theme", "dark", 4), get("theme", 4), get("theme", 5)
Output: ["light", "light", "dark", "dark"]
Why:    at time 3 the latest version is still the one written at 1
Input:  set("port", "80", 10), get("port", 9), get("port", 10)
Output: ["", "80"]
Why:    before time 10 the key has no value yet
Input:  get("missing", 7)
Output: [""]
Why:    edge case, a key that was never set

Hints

0 / 3

Stuck on the idea rather than the code? Binary Search covers it.