Implement map serialization and deserialization
Company: OpenAI
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Onsite
Quick Answer: This question evaluates understanding of serialization, string encoding and parsing, data structure invariants (invertibility), and efficiency when representing maps with arbitrary string keys and values.
Constraints
- 0 <= number of key-value pairs <= 50000
- The total number of characters across all keys and values is at most 10^6
- Keys and values are arbitrary strings and may contain delimiter-like characters, including '#'
- Keys and values do not contain the null character '\0'
- For deserialize, the input string is expected to follow the specified format; malformed input may raise ValueError
Examples
Input: ('serialize', {})
Expected Output: '0#'
Explanation: An empty map has 0 pairs, so the entire encoding is just the pair count followed by '#'.
Input: ('deserialize', '0#')
Expected Output: {}
Explanation: The pair count is 0, so the reconstructed dictionary is empty.
Input: ('serialize', {'': '', 'a:b|c': 'x,y=z', 'k#1': 'v#2'})
Expected Output: '3#0#0#5#a:b|c5#x,y=z3#k#13#v#2'
Explanation: The keys are serialized in sorted order: '', 'a:b|c', 'k#1'. Each key and value is written as length + '#' + content.
Input: ('deserialize', '3#0#0#5#a:b|c5#x,y=z3#k#13#v#2')
Expected Output: {'': '', 'a:b|c': 'x,y=z', 'k#1': 'v#2'}
Explanation: Length prefixes allow correct parsing even though the strings contain punctuation and '#'.
Input: ('serialize', {'b': 'two words', 'a': '1', '': ' '})
Expected Output: '3#0#1# 1#a1#11#b9#two words'
Explanation: Sorted key order is '', 'a', 'b'. The single-space value is encoded as length 1, and 'two words' has length 9.
Input: ('deserialize', '3#0#1# 1#a1#11#b9#two words')
Expected Output: {'': ' ', 'a': '1', 'b': 'two words'}
Explanation: Empty keys, space-only values, and multi-word values are reconstructed exactly.
Hints
- If keys and values can contain any delimiter character, splitting on a separator is not enough.
- Prefix each key and value with its length so the parser always knows exactly how many characters to read next.