v9.1.1
Menu

Time Based Key-Value Store

mediumPRODesign & ImplementationGoogle DSA

Design a key-value store that supports:

  • set(key, value, timestamp) - Store the key-value pair at the given timestamp.
  • get(key, timestamp) - Return the value with the largest timestamp <= given timestamp. Return "" if no such value.

You will receive a sequence of operations. Output the result of each get operation on a separate line.

Example

Input
set,foo,bar,1;set,foo,bar2,4;get,foo,1;get,foo,3;get,foo,4;get,foo,5
Output
bar
bar
bar2
bar2
Reasoning
  • The input sequence is split into operations: set(foo, bar, 1), set(foo, bar2, 4), get(foo, 1), get(foo, 3), get(foo, 4), get(foo, 5).
  • The set operations store key-value pairs at the given timestamps: (foo, bar, 1) and (foo, bar2, 4).
  • The get operations retrieve the value with the largest timestamp <= the given timestamp:
    • get(foo, 1) returns bar because 1≤11 \leq 1.
    • get(foo, 3) returns bar because 1≤31 \leq 3 and there's no value at timestamp 2.
    • get(foo, 4) returns bar2 because 4≤44 \leq 4.
    • get(foo, 5) returns bar2 because 4≤54 \leq 5 and there's no value at timestamp 5.
  • The results of the get operations are output on separate lines, producing the given output.

Constraints

  • 1 <= key.length, value.length <= 100
  • 1 <= timestamp <= 10^7
  • Timestamps for set are strictly increasing
  • At most 2 * 10^5 operations

Learn this in

More from Google DSA

PRO

Editor locked

The code editor is locked for Pro problems. It is only available for free problems. Please upgrade to gain access to the code editor for all problems.

solution.py

Test results

Sign in to run your code against the test cases.