Problem
Implement a simple key–value store that persists data on disk.
You must store the data in fixed-size shards, where each shard is saved in one file. If a shard file reaches its maximum size, new writes must go into a new shard file. The store must support shutdown (persist) and restore (recover).
You are given helper functions:
-
encode(key, value) -> bytes
to serialize a key/value record
-
decode(bytes) -> (key, value)
to deserialize a record
Assume keys and values are strings (or byte arrays), and encode/decode are inverses for valid records.
Required API
Design and implement (language-agnostic) functions/methods equivalent to:
-
put(key, value)
-
Insert or overwrite the value for
key
.
-
get(key) -> value | null
-
Return the current value for
key
, or
null
/
None
if missing.
-
delete(key)
(optional if you want to support removals)
-
shutdown()
-
Ensure all in-memory state is persisted so the store can be restored later.
-
restore(directory_path)
(or constructor-based restore)
-
Load persisted data from disk and make the store usable again.
Storage requirements
-
Data is stored across
one or more files
in a directory.
-
Each file is a
shard
with a configurable maximum size
SHARD_SIZE_BYTES
.
-
Writes append records to the current shard until it would exceed the shard size; then create a new shard file.
What to discuss / clarify
-
Your on-disk layout (file naming, record boundaries, handling partial/corrupt tail).
-
How you locate the latest value for a key after many overwrites.
-
What metadata or in-memory index (if any) you maintain.
-
Complexity of
put/get
and
restore
.
-
Edge cases: empty store, large values, shard rollover, overwrite semantics, optional delete semantics.