Skip to main content
All Questions

Write functions to serialize and deserialize a list of strings.

Medium
Unlock detailed company stats for this questionUpgrade

Serialize and Deserialize a List of Strings. You are tasked with writing two functions: serialize and deserialize. The serialize function will take a list of strings as input and return a single string. The deserialize function will take this single string and reconstruct the original list of strings.

Details

  • The serialize function should concatenate all strings in the list into a single string, with each original string's length encoded as a single character prefix.
  • The deserialize function should be able to take the single string and accurately split it back into the original list of strings, using the encoded length characters as guides.

For example, consider a list ["hello", "world"]. The serialization of this list might be "\x05hello\x05world", where \x05 indicates that the following string has a length of 5 characters.

Constraints

  • The maximum length of any string is 65535 (0xFFFF).
  • If a string's length exceeds the maximum, the serialize function should raise an exception.
  • The deserialize function should accurately handle any edge cases, such as empty strings or single-character strings.
  • Assume that all strings consist of ASCII characters only.

Examples

lst = ['hello', 'world'] serialized = serialize(lst) # serialized output: "\x05hello\x05world" deserialized = deserialize(serialized) # deserialized output should be: ['hello', 'world'] lst = [''] serialized = serialize(lst) # serialized output: "\x00" deserialized = deserialize(serialized) # deserialized output should be: [''] lst = ['a', 'bc', 'def'] serialized = serialize(lst) # serialized output: "\x01a\x02bc\x03def" deserialized = deserialize(serialized) # deserialized output should be: ['a', 'bc', 'def']

Related courses

Course

Software Engineering Interview Prep

Land your dream software engineering role at Google, Amazon, Microsoft, Meta, Apple, and other top companies. Learn from mock interviews, frameworks, and advice from senior candidates—practice data structures, algorithms, system design, people management, behavioral interviews, and more.

Course

System Design Interviews

Learn how to answer the latest system design questions across product, infrastructure, and AI domains. Features in-depth video examples, written breakdowns, and helpful reference patterns. Watch senior engineers and managers answer these questions in mock interview videos.