Problem · String

BitFont Part 3 - Decode Run-Length-Encoded Rows

MediumStripeONSITE INTERVIEW
See Stripe hiring insights

This continues the bitmap-font renderer. To save space, a font may store each glyph row Run-Length-Encoded (RLE). Decode the encoded rows back into full 0/1 strings.

Each encoded row is a sequence of run-length characters. The runs alternate pixel state and always start with off (0): the first run is off, the next on, the next off, and so on. Each run-length character maps to a count:

  • digits 0-9 mean lengths 0 through 9;
  • letters a-z mean lengths 10 through 35 (so a = 10, b = 11, ..., z = 35).

For each run character, emit that many copies of the current pixel value, then flip the state for the next run. A run of length 0 emits no pixels but still flips the state — do not special-case zero by skipping the flip, or every later run in that row inverts. All decoded rows end up the same width.

Examples
01 · Example 1
encodedRows = ["532", "19"]
return = ["0000011100", "0111111111"]
Row '532': 5 off (00000), 3 on (111), 2 off (00) gives 0000011100. Row '19': 1 off (0), 9 on (111111111) gives 0111111111. Both rows are width 10.
02 · Example 2
encodedRows = ["05", "a0"]
return = ["11111", "0000000000"]
Row '05': the first run length is 0 (off) which emits nothing but still flips to on, then 5 on gives 11111. Row 'a0': a = 10 off gives ten 0s, then a 0-length on run emits nothing. This exercises the zero-length-run flip trap.
03 · Example 3
encodedRows = ["3b"]
return = ["00011111111111"]
Row '3b': 3 off (000), then b = 11 on (11111111111) gives 00011111111111, a width of 14.
Constraints
  • Each encoded row is a string of run-length characters in 0-9 or a-z.
  • Runs alternate state starting from off (0): off, on, off, on, ...
  • 0-9 map to lengths 0-9; a-z map to lengths 10-35.
  • A length-0 run emits nothing but still flips the state.
  • All decoded rows have the same width.
More Stripe problems
drafts saved locally
public String[] decodeRle(String[] encodedRows) {
  // write your code here
}
encodedRows["532", "19"]
expected["0000011100", "0111111111"]
sign in to submit