Decode a Bitstream Using a Variable-Length Prefix Code Table

Last updated: November 29, 2026

Quick Overview

Given a table mapping symbols to binary codewords that form a prefix-free code, decode a string of bits into the symbol sequence, and report an error if the stream ends mid-codeword or contains an invalid code.

Google
Coding & Algorithms
Software Engineer
Google
November 29, 2026
Software Engineer
Technical Screen
Coding & Algorithms
Medium

0

0

0 solved


Given a table mapping symbols to binary codewords that form a prefix-free code, decode a string of bits into the symbol sequence, and report an error if the stream ends mid-codeword or contains an invalid code.

A Google technical screen question about compression internals, relevant to formats like Brotli and gzip used across Google serving. The interviewer checks building a binary decoding trie, walking it bit by bit, resetting at leaves, and validating the table itself.

What the Interviewer Expects
  • Build a binary trie from the code table
  • Validate that the table is prefix-free while building
  • Decode by walking bits and emitting at leaves
  • Detect invalid paths and truncated streams
  • Discuss table-driven decoding that reads several bits at once
Key Topics to Cover
binary trie
prefix codes
Huffman decoding
validation
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • How would you decode k bits at a time with a lookup table?
  • How would you build the code table from symbol frequencies (Huffman)?
  • How would you handle canonical Huffman codes transmitted as lengths only?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements

Input: table, a dict symbol -> codeword with codewords over 0/1, and bits, a string of 0/1. Output: the decoded string. Raise ValueError if the table is not prefix-free, if a bit path lead...

High-Level Design

Build a binary trie: for each symbol, walk its codeword, creating nodes, and store the symbol at the final node. While building, reject the table if we pass through a node that already holds a symbol ...


Submit Your Answer
Markdown supported