The Huffman Decoder 🌼
Updated
Problem statement
Huffman codes compress text by assigning the characters that occur at the highest frequency the shortest possible codes. In this encoding scheme, no code can be a prefix of another. For example, if the code for a is 01, then the code for b cannot be 011.
Given an array of Huffman code mappings and a Huffman-encoded string, find the decoded string. Each mapping will be a string consisting of a tab-separated ('\t') character and its encoded value: 'c encoded value' where the whitespace is a tab character. The newline character is represented as the character [newline] in the codes list, but should translate to \n when decoded.
Note: While all code mappings in the example are 6 digits long, mappings can be different lengths. The algorithm creates the shortest length mapping for the most frequent character encoded.
Function
decode(codes: String[], encoded: String) → String
Complete the function decode in the editor below. The function must return the decoded string.
decode has the following parameter(s):
string codes[n]: an array of character mappings in the form"character\tmapping"encoded: an encoded string
Examples
Example 1
codes = ["a\t100100", "b\t100101", "[newline]\t111111"]encoded = "100100111111100101"return = "a\nb"The output is "a\nb". There is a newline inbetween a and b.
Constraints
- 1 ≤ n ≤ 100
- 1 ≤ |encoded| ≤ 7000
- All characters of encoded are either '0' or '1'
- All inputs will represent a valid Huffman encoded string
- An encoded character may be a letter, space, [newline], or punctuation, e.g. dot, comma, etc.
More Wolverine Trading interview questions
Similar questions at other companies
- Suggested ProductsSquarepoint Capital · OASquarepoint Capital interview questions
- Find Length of Longest Common PrefixUber · OAUber interview questions
- Count Words with Each PrefixCapital One · OACapital One interview questions
- Max DistanceGoogle · OAGoogle interview questions
- Find All Word Occurrences in TextSentry · PHONE SCREENSentry interview questions
- Highlight and Rank Overlapping CitationsHarvey · PHONE SCREEN · ONSITE INTERVIEWHarvey interview questions
- Concatenated WordsAmazon · PHONE SCREEN · ONSITE INTERVIEW
- Keyboard Autocomplete SuggestionsLyft · ONSITE INTERVIEWLyft interview questions
- Design Search Autocomplete SystemRoblox · PHONE SCREEN · ONSITE INTERVIEWRoblox interview questions
- Transform Dotted Keys into a Nested MapProofpoint · ONSITE INTERVIEW