Implement delimiter-free string codec
Company: OpenAI
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Quick Answer: This question evaluates a candidate's ability to design reversible string encodings that handle arbitrary characters and edge cases, testing competency in string manipulation, data serialization, and robustness.
Constraints
- 0 <= number of strings <= 10^4
- 0 <= length of each string <= 10^6
- All characters are allowed inside the original strings, including digits, spaces, punctuation, and empty strings
- The input to `decode` is guaranteed to be a valid string produced by the encoder
- Use a delimiter-free strategy; do not depend on separator symbols appearing nowhere in the data
Examples
Input: ("encode", ["lint", "code", "love", "you"])
Expected Output: "0000000004lint0000000004code0000000004love0000000003you"
Explanation: Each word is stored as a 10-digit length followed by the word itself.
Input: ("decode", "0000000004lint0000000004code0000000004love0000000003you")
Expected Output: ["lint", "code", "love", "you"]
Explanation: Reading 10 characters at a time for lengths reconstructs the original list.
Hints
- If a separator can appear inside the data, store each string's length instead of searching for a marker.
- Make decoding simpler by using a fixed-size length header, so you always know exactly where to read the next length.