Key-Value Store Codec

Asked at OpenAI· Screen

Reported by the problem's creator, not verified

Import another question

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.

Examples

  1. Example 1

    Input: 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"}]

    Explanation: 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:.

Import your own questions
Paste a question you were asked and get a full problem: statement, tests and a checked solution.
Import a question

Code

Loading editor…

Ln 1, Col 1

Start an attempt to use Coach.