Segmented Durable Key-Value Store
Problem statement
Simulate a durable string key-value store over an ordered batch of commands. The durable store is an append-only log split greedily across numbered segments, each with capacity 1024 bytes.
Commands
PUT key value: append a record and setkeytovalueusing last-write-wins semantics. ReturnOK. The value is the entire suffix after the second space and may be empty.GET key: returnVALUE:valuefor a present key, orNOT_FOUND.RELOAD: discard the in-memory map and reconstruct it by replaying every complete record in segment order. ReturnOK.SEGMENTS: returnSEGMENTS:s1,s2,..., where each value is the used-byte count of one durable segment in order. With no records, returnSEGMENTS:.
Record encoding and segmentation
All keys and values are printable ASCII, so one character occupies one byte. Encode a record as keyLength:keyvalueLength:value, where each length is written in base 10. A record is never split. Before an append that would make the current segment exceed 1024 bytes, start a new segment. Every encoded record is guaranteed to fit in one segment.
Examples
Example 1
commands = ["PUT color blue","GET color","PUT color green","RELOAD","GET color","SEGMENTS"]return = ["OK","VALUE:blue","OK","OK","VALUE:green","SEGMENTS:27"]The two encoded records use 13 and 14 bytes. Reload replays both records, so the later value green wins.
Unlock this recently reported problem
FastPrep Pro gives you full access to interview problems reported within the last week.
- Full problem statement and constraints
- 1 more worked example, explained
- Guided hints and editorial
- Run your code on real test cases
Pro subscription, billed yearly — or $19 month-to-month. Cancel anytime.