OpenAI questions

Key-Value Store Codec

OpenAIPhone screenMedium

A string s is written as its length in characters, a colon, then s itself: "hi" becomes 2:hi and "" becomes 0:. A record is a key string immediately followed by a value string, so {"a": "xyz"} is the record 1:a3:xyz. Keys and values may contain any characters, including colons, commas, digits, newlines and non-ASCII characters. Dictionaries keep insertion order, and every operation must preserve it.

Part 1: Serialize and deserialize

Given a dictionary data with string keys and values, serialize(data) returns the concatenation of one record per pair, in the dictionary's order; an empty dictionary gives "".

deserialize(blob) takes a string that serialize produced and returns the same pairs as a dictionary, in the same order.

  1. Example 1
    init
    []
    operations
    [["serialize",[{"name":"John:Doe","city":"New,York"}]],["deserialize",["4:name8:John:Doe4:city8:New,York"]],["serialize",[{}]],["deserialize",[""]],["serialize",[{"":""}]],["deserialize",["0:0:"]],["serialize",[{"k":""}]],["deserialize",["1:a1:11:b2:22"]]]
    Output
    ["4:name8:John:Doe4:city8:New,York",{"name":"John:Doe","city":"New,York"},"",{},"0:0:",{"":""},"1:k0:",{"a":"1","b":"22"}]

    Why: Each string is written as its length, a colon, then the characters: name has 4 characters and John:Doe has 8. The colon and comma inside values are just data. An empty dictionary is the empty string, and an empty key or value is written 0:.

Constraints

  • 0 <= len(data) <= 10^3
  • 0 <= len(key), len(value) <= 10^3
  • blob is always a valid output of serialize