1-bit and 2-bit Characters

EasyArrays & Hashinggreedy

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

A special encoding has two kinds of characters: a one-bit character written as 0, and two-bit characters written as 10 or 11. You are given a bit array bits that is a valid encoding and always ends with 0. Decoding left to right is unambiguous. Return true if the final character is a one-bit character, and false if the final 0 is the second half of a two-bit character 10.

Examples

Input: bits = [1,0,0]
Output: true
It decodes as `10` then `0`, so the last character is one bit.
Input: bits = [1,1,1,0]
Output: false
It decodes as `11` then `10`, so the last character is two bits.

Constraints

  • 1 <= bits.length <= 1000
  • `bits[i]` is `0` or `1`
  • `bits[bits.length - 1] == 0`
  • `bits` is a valid encoding

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More greedy problems