Run-Length and Bit-Packing Codec

Asked at DatabricksOnsite

Reported by the problem's creator, not verified

Build Codec, a small compression scheme for streams of unsigned integers. The constructor takes bit_width; every value in a stream is in [0, 2^bit_width - 1]. A compressed stream is a list of block strings of two kinds:

  • RLE:<value>:<count> stands for count copies of value.
  • BP:<count>:<bits> stands for count values written one after another, each as exactly bit_width binary digits with leading zeros, so bits has count * bit_width digits.

Each test drives one Codec(bit_width) instance through a sequence of calls and checks every return value.

Examples

  1. Example 1

    Input: init = [2], operations = [["encode",[[3,3,3,3,3,3,3,3,1,2,3]]],["encode",[[1,2,1,2,1,2,1,2,3,3,3,3,3,3,3,3,0]]],["encode",[[1,1,1,1,1,1,1]]]]

    Output: [["RLE:3:8","BP:3:011011"],["BP:8:0110011001100110","RLE:3:8","BP:1:00"],["BP:7:01010101010101"]]

    Explanation: Eight 3s in a row form a maximal run, so they become RLE:3:8; the remaining 1, 2, 3 are packed two bits each as 01 10 11. In the second stream the first eight values fill one packed block, the eight 3s are a run, and the final 0 is a packed block of one. Seven equal values are below the run threshold, so they stay packed as seven copies of 01.

Import your own questions
Paste a question you were asked and get a full problem: statement, tests and a checked solution.
Import a question

Code

Loading editor…

Ln 1, Col 1

Start an attempt to use Coach.