Encode a list of strings into one string and decode it back losslessly

Quick Overview

Design a pair of functions that turn a list of arbitrary strings into one string and recover the exact list from it. It tests designing a lossless format, handling empty strings and delimiter characters inside the content, and parsing the result in linear time.

Encode a list of strings into one string and decode it back losslessly

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

Develop an algorithm to convert a list of strings into a single encoded string, such that it can be decoded back into the original list of strings without any loss of information. Implement `encode` and `decode` functions: - `encode` takes a list of strings and returns a single encoded string. - `decode` takes an encoded string and returns the original list of strings. You choose the encoding format. The strings may contain any character, so the format has to survive strings that contain whatever separator or marker you pick. ### Function Signature ```python class Codec: def encode(self, strs: list[str]) -> str: ... def decode(self, s: str) -> list[str]: ... ``` ### Rules - The checker calls `encode(strs)`, passes the returned string to `decode`, and compares the list `decode` returns with `strs`. The lists must be equal: same length, same order, same characters, empty strings included. - Any encoding format is accepted as long as that round trip is exact. The encoded string itself is never compared with anything. - `decode` must rebuild the list from the encoded string alone. Do not keep the list in an instance attribute, class attribute or global between the two calls. ### Constraints - `0 <= strs.length < 150` - `0 <= strs[i].length < 250` - Only valid UTF-8 characters are contained in `strs[i]` ### Examples ```text Input: strs = ["read", "write", "run", "test"] Output: ["read", "write", "run", "test"] ``` `decode(encode(strs))` returns the four original strings in order. One valid encoded string is `"4#read5#write3#run4#test"`, though the checker never looks at it. ```text Input: strs = ["how", "you", "?", "doing"] Output: ["how", "you", "?", "doing"] ``` The round trip returns the same list. The one-character string `"?"` comes back with nothing added or lost. ```text Input: strs = ["", "5#a#b", "12"] Output: ["", "5#a#b", "12"] ``` The list holds an empty string, a string that contains `#` and looks like a length prefix, and a string made only of digits. A correct format still returns exactly three strings, each unchanged.

Overview: Design a pair of functions that turn a list of arbitrary strings into one string and recover the exact list from it. It tests designing a lossless format, handling empty strings and delimiter characters inside the content, and parsing the result in linear time.

General logo
General
Nov 25, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Develop an algorithm to convert a list of strings into a single encoded string, such that it can be decoded back into the original list of strings without any loss of information.

Implement encode and decode functions:

  • encode takes a list of strings and returns a single encoded string.
  • decode takes an encoded string and returns the original list of strings.

You choose the encoding format. The strings may contain any character, so the format has to survive strings that contain whatever separator or marker you pick.

Function Signature

class Codec:
    def encode(self, strs: list[str]) -> str:
        ...

    def decode(self, s: str) -> list[str]:
        ...

Rules

  • The checker calls encode(strs) , passes the returned string to decode , and compares the list decode returns with strs . The lists must be equal: same length, same order, same characters, empty strings included.
  • Any encoding format is accepted as long as that round trip is exact. The encoded string itself is never compared with anything.
  • decode must rebuild the list from the encoded string alone. Do not keep the list in an instance attribute, class attribute or global between the two calls.

Constraints

  • 0 <= strs.length < 150
  • 0 <= strs[i].length < 250
  • Only valid UTF-8 characters are contained in strs[i]

Examples

Input: strs = ["read", "write", "run", "test"]
Output: ["read", "write", "run", "test"]

decode(encode(strs)) returns the four original strings in order. One valid encoded string is "4#read5#write3#run4#test", though the checker never looks at it.

Input: strs = ["how", "you", "?", "doing"]
Output: ["how", "you", "?", "doing"]

The round trip returns the same list. The one-character string "?" comes back with nothing added or lost.

Input: strs = ["", "5#a#b", "12"]
Output: ["", "5#a#b", "12"]

The list holds an empty string, a string that contains # and looks like a length prefix, and a string made only of digits. A correct format still returns exactly three strings, each unchanged.