Serialize a List of Chat Messages Into One String and Back
Last updated: November 29, 2026
Quick Overview
Design encode(messages) that turns a list of arbitrary message strings into a single string, and decode(s) that recovers the exact original list, where messages may contain any characters including delimiters, digits, and empty strings.
Meta
November 29, 20260
0
0 solved
Design encode(messages) that turns a list of arbitrary message strings into a single string, and decode(s) that recovers the exact original list, where messages may contain any characters including delimiters, digits, and empty strings.
Messenger and WhatsApp batch messages into compact wire formats, and this question checks whether a candidate can design a framing scheme that is unambiguous for any content. Interviewers look for length-prefix framing and an explanation of why delimiter-only schemes break.
What the Interviewer Expects
- Show why a plain delimiter fails when messages contain it
- Use length-prefixed framing such as 5#hello
- Parse the length up to the separator, then slice exactly that many characters
- Round-trip empty strings and the empty list
- Discuss escaping as an alternative and its costs
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- How would you make this binary and compact (varints)?
- How would you stream-decode when data arrives in chunks?
- How would you detect a corrupted or truncated payload?
- How does this compare to protobuf framing?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
- Messages are strings with any characters (including
#, digits, newlines). decode(encode(x)) == xfor every list, including[]and[''].- Linear time in total length....
High-Level Design
Delimiters alone fail: joining with | cannot represent a message containing | without escaping. Length-prefix framing avoids escaping entirely. Each message becomes str(len(m)) + '#' + m.
To de...