KatInterview

Reversible String List Serializer

StringsAuto-graded · 21 test cases
MediumAuto-gradedStringsstringsserializationencoding

Last updated

Tests whether a candidate can design a lossless encode/decode round-trip for arbitrary strings without relying on a fixed delimiter character.

Reversible String List Serializer

You need to pack a list of strings into a single string and then recover the original list exactly — including empty strings and strings that contain any character imaginable (colons, newlines, null bytes, emoji, you name it).

Your Task

Implement a function serialize_strings(strings) that:

  1. Encodes the input list into one string.
  2. Decodes that string back to the original list.
  3. Returns the recovered list.

Internally you will write two helpers — an encoder and a decoder — but the graded function is just the round-trip:

def serialize_strings(strings: List[str]) -> List[str]:
    ...

Why a Plain Separator Won't Work

If you join strings with , or | and then split on it, you break the moment any string contains that character. You cannot reserve a "safe" separator, because the input strings are unconstrained.

The Right Approach: Length-Prefix Framing

Instead of marking where strings end, tell the decoder how many characters to read. For each string s, write its character count followed by a fixed marker, then the string verbatim:

<length><marker><payload>

Because the decoder already knows how long the payload is before it starts reading it, it never has to scan inside the payload for a boundary. The marker character can appear freely inside any string — the decoder never looks for it there.

Example (using <length>:<payload> framing):

Input listEncoded blob
["hi", ""]2:hi0:
["3:x"]3:3:x
[]`` (empty string)

Notice the third row: an empty input list encodes to an empty blob. A list containing one empty string encodes to "0:" — these two cases must be distinct.

Constraints

  • 0 ≤ len(strings) ≤ 200
  • 0 ≤ len(strings[i]) ≤ 200
  • strings[i] may contain any Unicode character, including digits, colons, newlines, null bytes, or characters that look like length prefixes
  • The round-trip must be exact: decode(encode(strings)) == strings for every valid input
  • An empty list and a list containing one empty string are different inputs and must produce different blobs

Examples

Example 1

Input: serialize_strings(['hello', 'world'])

Output: ['hello', 'world']

Example 2

Input: serialize_strings(['', 'a:b', 'x\\ny'])

Output: ['', 'a:b', 'x\\ny']

Example 3

Input: serialize_strings([])

Output: []

Hints

Hint 1

Before writing any code, ask: what information does the decoder need at each step so it never has to inspect the payload content to find a boundary?

Hint 2

A length prefix puts that information up front. For each string, write its length (as a decimal number), then a fixed single-character marker, then the raw string. On decode, read digits until the marker, parse the length L, then take exactly the next L characters as the payload — no scanning, no ambiguity.

Hint 3

Watch the two tricky edge cases: an empty list should encode to an empty string, and a list containing a single empty string should encode to something non-empty (e.g. '0:'). Make sure your decoder loop terminates correctly when the blob is empty and that it handles a zero-length payload without skipping anything.