Beyond Big O

Expiring key-value store

Expiring key-value store

Medium

Part 1 only.

Solution guide

Reveal solution guide — spoilers

Approach

  1. Store each key and its current value in a map owned by this instance.
  2. Replace an existing entry on a write. Distinguish a missing key from an empty stored string.

Why it works

The map contains exactly the latest value for each present key, so lookups and replacements agree.

Complexity

Expected O(1) lookup and write with bounded key length, using O(k) storage for k entries.

Edge cases to check

  • Check missing and case-sensitive keys, empty values, replacement, and independent instances.
Editor options
Typing preferences

Saved in this browser.

Restoring local drafts…