Skip to main content

Time Based Key-Value Store

Part 1: Simple Cache - set(key, value): store a value - get(key): return value for key, or "" if missing

Implement a store with string keys and values, processing arrays of commands and their arguments and returning the string from the last lookup, or an empty string if there are no lookups. Part 1 provides set(key,value), which overwrites the previous value, and get(key), which returns the current value or an empty string for a missing key. Part 2 adds setTime(key,value,timestamp) and getTime(key,timestamp), using integer timestamps that arrive in strictly increasing order for each key. A timed lookup returns the value at the greatest stored timestamp no later than the query, or an empty string if none exists. Part 3 keeps the same behavior and write ordering but requires O(log n) lookup time for n stored timestamps of a key. Part 4 allows out-of-order and duplicate timestamps, with the most recent write winning when a key and timestamp are repeated.

Examples

You write

TimeMap: set(key: str, value: str), get(key: str)

  1. Example 1

    Input
    set("foo", "bar"), get("foo")
    returns
    "bar"
  2. Example 2

    Input
    set("a", "1"), set("b", "2"), get("a")
    returns
    "1"

The pattern behind it

Arrays & Hashing

I reach for it when

  • You catch yourself scanning the array again for every element.
  • The problem asks about duplicates, pairs, counts or groups.

Say this before you type. Say what the key is and what the value remembers, before you write the loop.

All 127 Arrays & Hashing problems

Binary Search

I reach for it when

  • The input is sorted, or sorted and then rotated.
  • You want a boundary: the first or last place something is true.

Say this before you type. Say the yes-or-no question, and which side each answer throws away.

All 19 Binary Search problems

Meet Coco

Coco is an AI study partner, not a person. She listens while you explain your code, asks what a line does and never hands you the fix. Sign in free to type with Coco, up to a daily limit. The editor and Run stay open without an account. Anyone can open the editor and run their code.

1Simple Cacheeasy
2Part 2
3Part 3
4Part 4

Each locked part opens in the editor once the part before it passes.

Prep guides that list this problem

Our company tags come from public interview write-ups. None of these is a confirmed question.