Implement lossless dictionary encoding and decoding
Problem Description
Dictionary encoding is a lossless compression technique applied to a list of tokens. You are tasked with implementing two functions to perform this encoding and its subsequent decoding.
The input will be a single string consisting of words separated by commas.
Example Input: "USA,USA,USA,USA,Mexico,Canada,Mexico,Mexico,Mexico"
Requirements
-
encode(input_string) -> compressed_string- Dictionary Construction: Build a dictionary of unique words. The order of words in this dictionary must be determined by their first appearance in the input string.
- Word Replacement: Replace each occurrence of a word in the input with its 0-based index from the constructed dictionary.
- Output Format: The
compressed_stringmust be a single string formatted asdictionary_part:index_part.dictionary_part: The unique words from the dictionary, joined by commas.index_part: The corresponding indices (as integers), joined by commas.
- Constraint: The
decodefunction will receive only thiscompressed_string. No additional data may be passed.
-
decode(compressed_string) -> original_string- Reconstruct the exact original comma-separated string from the given
compressed_string.
- Reconstruct the exact original comma-separated string from the given
Example
- Input:
"USA,USA,USA,USA,Mexico,Canada,Mexico,Mexico,Mexico" - Encoded Output:
"USA,Mexico,Canada:0,0,0,0,1,2,1,1,1" - Decoded Output:
"USA,USA,USA,USA,Mexico,Canada,Mexico,Mexico,Mexico"
Edge Cases
- Handle empty input strings appropriately for both
encodeanddecode.
AI interview prep
Core dilemma
Efficiently mapping tokens to indices while preserving insertion order for dictionary construction, and then correctly serializing/deserializing this mapping and the token sequence into a single, specific string format without loss of information or performance bottlenecks.
3 tradeoffs and 3 stress tests locked
Interview prep
Unlock the interviewer X-Ray rubric, tradeoff matrix, stress tests, clarifying questions, and key concepts.
Implement lossless dictionary encoding and decoding
Problem Description
Dictionary encoding is a lossless compression technique applied to a list of tokens. You are tasked with implementing two functions to perform this encoding and its subsequent decoding.
The input will be a single string consisting of words separated by commas.
Example Input: "USA,USA,USA,USA,Mexico,Canada,Mexico,Mexico,Mexico"
Requirements
-
encode(input_string) -> compressed_string- Dictionary Construction: Build a dictionary of unique words. The order of words in this dictionary must be determined by their first appearance in the input string.
- Word Replacement: Replace each occurrence of a word in the input with its 0-based index from the constructed dictionary.
- Output Format: The
compressed_stringmust be a single string formatted asdictionary_part:index_part.dictionary_part: The unique words from the dictionary, joined by commas.index_part: The corresponding indices (as integers), joined by commas.
- Constraint: The
decodefunction will receive only thiscompressed_string. No additional data may be passed.
-
decode(compressed_string) -> original_string- Reconstruct the exact original comma-separated string from the given
compressed_string.
- Reconstruct the exact original comma-separated string from the given
Example
- Input:
"USA,USA,USA,USA,Mexico,Canada,Mexico,Mexico,Mexico" - Encoded Output:
"USA,Mexico,Canada:0,0,0,0,1,2,1,1,1" - Decoded Output:
"USA,USA,USA,USA,Mexico,Canada,Mexico,Mexico,Mexico"
Edge Cases
- Handle empty input strings appropriately for both
encodeanddecode.