InferenceAI

Enter at least two characters to search.

Start free

Time-Keyed Store

mediumDesign25 min25 XP

Simulate a key-value store with history. ["set", key, value, t] stores value for key at timestamp t (timestamps for a key strictly increase). ["get", key, t] returns the value with the largest timestamp ≤t\le t for that key, or "" if there is none. Return the results of all get operations.

Requirements

  • Binary search the timestamps of a key: O(log⁡n)O(\log n) per get.

Examples

EXAMPLE WALKTHROUGHclassic

A guided view of the published example, not a trace of your code.

INPUT[[["set","foo","bar",1],["get","foo",1],["get","foo",3],["set","foo","bar2",4],["get","foo",4],["get","foo",5]]]
TASKtime_keyed_store()
EXPECTED OUTPUT["bar","bar","bar2","bar2"]

Start with the input values shown in this example.

Example 1 of 3

Input: operations = [["set","foo","bar",1],["get","foo",1],["get","foo",3],["set","foo","bar2",4],["get","foo",4],["get","foo",5]]

Output: ["bar","bar","bar2","bar2"]

Explanation: classic

Input: operations = [["get","x",10]]

Output: [""]

Explanation: missing key

Input: operations = [["set","a","v1",5],["get","a",4]]

Output: [""]

Explanation: too early

Hint 1

Keep, per key, parallel lists of timestamps and values.

Hint 2

bisect_right(times, t) - 1 gives the latest index at or before t.

Topics

DSADesignPython