FastPrepIn-Memory Key-Value Store Serialization

In-Memory Key-Value Store Serialization

OpenAI logoOpenAI● MediumFULLTIMEPHONE SCREEN
Learn

Problem statement

Process an ordered sequence of operations on an initially empty in-memory string key-value store:

  • ["PUT", key, value] stores or overwrites one value and returns "OK".
  • ["GET", key] returns "VALUE:" + value, or "NOT_FOUND".
  • ["SERIALIZE"] returns a deterministic snapshot.
  • ["DESERIALIZE", snapshot] replaces the entire current store with the decoded snapshot and returns "OK".

Encode every key and value as eight uppercase hexadecimal digits containing its 32-bit character length, followed immediately by the characters. A snapshot concatenates encoded key/value pairs in lexicographic key order. The empty store serializes to the empty string. Every deserialization input is a valid snapshot produced by this format.

Return one result for every operation.

Function

processSerializedStore(operations: String[][]) → String[]

Examples

Example 1

operations = [["PUT","b","two"],["PUT","a","one"],["SERIALIZE"],["GET","a"]]return = ["OK","OK","00000001a00000003one00000001b00000003two","VALUE:one"]

Serialization orders key a before key b. Each length occupies exactly eight hexadecimal digits.

Example 2

operations = [["PUT","x:y",""],["SERIALIZE"],["DESERIALIZE","00000001a00000003one"],["GET","x:y"],["GET","a"]]return = ["OK","00000003x:y00000000","OK","NOT_FOUND","VALUE:one"]

Length framing safely carries punctuation and an empty value. Deserialization replaces the old map, so x:y disappears and a maps to one.

Constraints

  • 1 <= operations.length <= 2000.
  • Keys are non-empty printable ASCII strings of length at most 100; values are printable ASCII strings of length at most 1000.
  • Every command has the exact fields described above.
  • Every supplied snapshot is valid, and the total input size is at most 200000 characters.

More OpenAI problems

See OpenAI hiring insights
public String[] processSerializedStore(String[][] operations) {
    // Write your code here.
}
operations[["PUT","b","two"],["PUT","a","one"],["SERIALIZE"],["GET","a"]]
expected["OK", "OK", "00000001a00000003one00000001b00000003two", "VALUE:one"]
Checking account…