Skip to main content

Serialize and Deserialize Strings

MediumPremium

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']