Implement an In-Memory Key-Value Store with Put, Get and Update

Quick Overview

Implement a small in-memory key-value store class with put, get and update methods over string keys and values. It tests how you separate insert-or-overwrite from update-only semantics, return sentinel values for missing keys, and reason about hash map complexity.

Implement an In-Memory Key-Value Store with Put, Get and Update

Role: Software Engineer

Category: Software Engineering Fundamentals

Difficulty: easy

Interview Round: Other

Design an in-memory key-value store that supports basic CRUD-style operations on string keys and string values. The store must support inserting new key-value pairs, retrieving values by key, and updating existing keys. Retrieval of a missing key returns a sentinel value, and updating a missing key returns an error indicator. Implement a class `KeyValueStore` with the following methods: - `put(key: str, value: str)`: Insert or overwrite the key-value pair in the store. - `get(key: str) -> str`: Return the value associated with `key`, or the string `"null"` if the key does not exist. - `update(key: str, value: str) -> str`: Update the value for an existing `key` and return `"OK"`. If the key does not exist, return `"ERROR"` and leave the store unchanged. The examples list the calls in order, then the arguments of each call, then the value each call returns (`null` for the constructor and for `put`). Example 1: ```text Input: ["KeyValueStore", "put", "get", "update", "get", "update", "get"] [[], ["name", "alice"], ["name"], ["name", "bob"], ["name"], ["age", "30"], ["age"]] Output: [null, null, "alice", "OK", "bob", "ERROR", "null"] Explanation: kv = KeyValueStore() # empty store kv.put("name", "alice") # store: {name: alice} kv.get("name") # -> "alice" kv.update("name", "bob") # -> "OK" (key exists, value updated) kv.get("name") # -> "bob" kv.update("age", "30") # -> "ERROR" (key does not exist) kv.get("age") # -> "null" (still missing) ``` Example 2: ```text Input: ["KeyValueStore", "get", "update", "put", "put", "get", "update", "get"] [[], ["x"], ["x", "1"], ["x", "10"], ["y", "20"], ["x"], ["y", "200"], ["y"]] Output: [null, "null", "ERROR", null, null, "10", "OK", "200"] Explanation: kv = KeyValueStore() # empty store kv.get("x") # -> "null" (no such key) kv.update("x", "1") # -> "ERROR" (key missing) kv.put("x", "10") # store: {x: 10} kv.put("y", "20") # store: {x: 10, y: 20} kv.get("x") # -> "10" kv.update("y", "200") # -> "OK" (y updated) kv.get("y") # -> "200" ``` ```hint Where the methods differ All three methods need fast lookup by a string key. Compare what each one must do when the key is missing, and work out which of them has to check before it writes. ``` ### Constraints & Assumptions - `1 <= key.length, value.length <= 100` - `key` and `value` consist of printable ASCII characters. - At most `10^4` calls in total to `put`, `get`, and `update`. ### Clarifying Questions to Ask - Can a stored value be the literal string `"null"`, and if so, do callers need to tell it apart from a missing key? - CRUD usually includes delete. Is a delete operation in scope, or only `put`, `get` and `update`? - Will more than one thread call the store at the same time? - Are keys compared exactly, with case and whitespace significant? ### What a Strong Answer Covers - The backing data structure and the expected cost of each operation on it. - How `put` and `update` differ on a missing key, including leaving the store unchanged when `update` fails. - How `get` handles a missing key without raising, and the ambiguity of the `"null"` sentinel. - Time and space complexity in terms of the number of stored keys. ### Follow-up Questions - How would you add `delete`, and what should it return for a key that does not exist? - What changes if several threads call the store at once, and which method has a check-then-write race? - How would you support a per-key expiry time? - How would you cap memory use, and which entries would you evict first?

Overview: Implement a small in-memory key-value store class with put, get and update methods over string keys and values. It tests how you separate insert-or-overwrite from update-only semantics, return sentinel values for missing keys, and reason about hash map complexity.

General logo
General
Nov 25, 2026
easySoftware EngineerOtherSoftware Engineering Fundamentals
0
0

Design an in-memory key-value store that supports basic CRUD-style operations on string keys and string values.

The store must support inserting new key-value pairs, retrieving values by key, and updating existing keys. Retrieval of a missing key returns a sentinel value, and updating a missing key returns an error indicator.

Implement a class KeyValueStore with the following methods:

  • put(key: str, value: str) : Insert or overwrite the key-value pair in the store.
  • get(key: str) -> str : Return the value associated with key , or the string "null" if the key does not exist.
  • update(key: str, value: str) -> str : Update the value for an existing key and return "OK" . If the key does not exist, return "ERROR" and leave the store unchanged.

The examples list the calls in order, then the arguments of each call, then the value each call returns (null for the constructor and for put).

Example 1:

Input:
["KeyValueStore", "put",             "get",    "update",        "get",    "update",      "get"]
[[],              ["name", "alice"], ["name"], ["name", "bob"], ["name"], ["age", "30"], ["age"]]

Output: [null, null, "alice", "OK", "bob", "ERROR", "null"]

Explanation:
kv = KeyValueStore()              # empty store
kv.put("name", "alice")           # store: {name: alice}
kv.get("name")                    # -> "alice"
kv.update("name", "bob")          # -> "OK"     (key exists, value updated)
kv.get("name")                    # -> "bob"
kv.update("age", "30")            # -> "ERROR"  (key does not exist)
kv.get("age")                     # -> "null"   (still missing)

Example 2:

Input:
["KeyValueStore", "get", "update",   "put",       "put",       "get", "update",     "get"]
[[],              ["x"], ["x", "1"], ["x", "10"], ["y", "20"], ["x"], ["y", "200"], ["y"]]

Output: [null, "null", "ERROR", null, null, "10", "OK", "200"]

Explanation:
kv = KeyValueStore()              # empty store
kv.get("x")                       # -> "null"   (no such key)
kv.update("x", "1")               # -> "ERROR"  (key missing)
kv.put("x", "10")                 # store: {x: 10}
kv.put("y", "20")                 # store: {x: 10, y: 20}
kv.get("x")                       # -> "10"
kv.update("y", "200")             # -> "OK"     (y updated)
kv.get("y")                       # -> "200"

Constraints & Assumptions

  • 1 <= key.length, value.length <= 100
  • key and value consist of printable ASCII characters.
  • At most 10^4 calls in total to put , get , and update .

Clarifying Questions to Ask Guidance

  • Can a stored value be the literal string "null" , and if so, do callers need to tell it apart from a missing key?
  • CRUD usually includes delete. Is a delete operation in scope, or only put , get and update ?
  • Will more than one thread call the store at the same time?
  • Are keys compared exactly, with case and whitespace significant?

What a Strong Answer Covers Guidance

  • The backing data structure and the expected cost of each operation on it.
  • How put and update differ on a missing key, including leaving the store unchanged when update fails.
  • How get handles a missing key without raising, and the ambiguity of the "null" sentinel.
  • Time and space complexity in terms of the number of stored keys.

Follow-up Questions Guidance

  • How would you add delete , and what should it return for a key that does not exist?
  • What changes if several threads call the store at once, and which method has a check-then-write race?
  • How would you support a per-key expiry time?
  • How would you cap memory use, and which entries would you evict first?