Problem · Design

Segmented Durable Key-Value Store

MediumOpenAI logoOpenAIFULLTIMEPHONE SCREEN

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 set key to value using last-write-wins semantics. Return OK. The value is the entire suffix after the second space and may be empty.
  • GET key: return VALUE:value for a present key, or NOT_FOUND.
  • RELOAD: discard the in-memory map and reconstruct it by replaying every complete record in segment order. Return OK.
  • SEGMENTS: return SEGMENTS:s1,s2,..., where each value is the used-byte count of one durable segment in order. With no records, return SEGMENTS:.

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.

The problem statement continues
Pro

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.

FastPrep Pro
Reported in 1 OpenAI interview this week

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
$9/month

Pro subscription, billed yearly — or $19 month-to-month. Cancel anytime.

Free plan — 2 of 2 free unlocks used this week
CodePython 3
Run and Submit unlock with Pro
FastPrep Pro
Reported in 1 OpenAI interview this week

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
$9/month

Pro subscription, billed yearly — or $19 month-to-month. Cancel anytime.

Free plan — 2 of 2 free unlocks used this week