← Questions

Key-value store with prefix search

0
SnowflakeCoding

A coding interview question asked at Snowflake. It tests trie, hash map and design.

Build an in-memory key-value store whose values can also be looked up by key prefix. Keys are strings and values are integers.

Implement the KVStore class:

  • KVStore() creates an empty store.
  • set(key, value) adds a new pair. Raise an error if key is already stored.
  • get(key) -> int returns the value for key, or -1 if key is not stored.
  • update(key, value) replaces the value of a stored key. Raise an error if key is not stored.
  • delete_key(key) removes key. Do nothing if it is not stored.
  • search(prefix) -> List[int] returns the values of all stored keys that start with prefix, ordered by key (lexicographic). An empty prefix matches every key. Return an empty list when nothing matches.

Prefix queries are frequent, so search should not scan the whole store. Its cost should depend on the prefix length and the size of the result, not on the total number of keys.

Constraints

  • Up to 10^5 operations in total.
  • Keys are 0 to 50 characters long; the empty string is a valid key.
  • Values fit in a 32-bit signed integer.

Example

store = KVStore()
store.set("pen", 4)
store.set("pencil", 2)
store.set("pet", 8)
store.set("map", 6)

store.search("pe")     -> [4, 2, 8]   # pen, pencil, pet
store.search("pen")    -> [4, 2]
store.get("pet")       -> 8
store.update("pen", 5)
store.delete_key("pencil")
store.search("pen")    -> [5]
store.get("pencil")    -> -1
store.search("z")      -> []
store.set("map", 1)    -> error: key exists

Interviewers often add parts one at a time, such as the sum of values under a prefix, returning only the first k matches, nested transactions with rollback, or making the store safe for concurrent use.

Clarifying questions

  • Is the empty string a valid key, and does search("") return its value too?
  • Does "ordered by key" mean plain code-point order?
  • Should update and set raise a particular error type?
  • How often do writes happen compared with searches?

What a strong answer covers

  • Keeps a hash map for O(1) get and a trie whose nodes hold the value of the key that ends there.
  • search(prefix) walks down the prefix, then visits the subtree with children in sorted order. That costs O(len(prefix) + size of the subtree), and it should not hit dead branches.
  • delete_key clears the value and prunes nodes that no longer lead to any key, so searches do not pay for deleted data.
  • Compares this with a sorted list and binary search: searches are as fast, but each insert and delete costs O(n).
  • Tests the empty key, a key that is a prefix of another (pen and pencil), deleting and re-adding a key, the error cases, and a prefix that matches nothing.

Follow-ups

  • The sum of the values under a prefix in O(len(prefix)): keep a subtree sum on each node.
  • Return only the first k matches.
  • Nested transactions with begin, commit and rollback.
  • Make the store safe for concurrent readers and writers.

Where it's asked

What it tests

trie · hash map · design

Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding