Key-value store with prefix search
I was asked this
No reports yet. Sign in to say where you were asked it.
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 ifkeyis already stored. -
get(key) -> intreturns the value forkey, or-1ifkeyis not stored. -
update(key, value)replaces the value of a storedkey. Raise an error ifkeyis not stored. -
delete_key(key)removeskey. Do nothing if it is not stored. -
search(prefix) -> List[int]returns the values of all stored keys that start withprefix, 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
updateandsetraise a particular error type? - How often do writes happen compared with searches?
What a strong answer covers
- Keeps a hash map for O(1)
getand 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_keyclears 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 (
penandpencil), 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
kmatches. - Nested transactions with
begin,commitandrollback. - Make the store safe for concurrent readers and writers.
Where it's asked
What it tests
Sign in to see the worked answer
The full answer, chapter by chapter, with what interviewers expect at each level. New accounts get 5 credits: each one unlocks a question for good.
What the labs asked this week
A weekly email of what OpenAI, Anthropic and the other top AI labs asked engineers, round by round.
Sign in to submit your code; every attempt is kept here.
