Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Build confidence with an intuition-first walkthrough focused on array fundamentals.
We have two special characters:
0.10 or 11).Given a binary array bits that ends with 0, return true if the last character must be a one-bit character.
Example 1:
Input: bits = [1,0,0] Output: true Explanation: The only way to decode it is two-bit character and one-bit character. So the last character is one-bit character.
Example 2:
Input: bits = [1,1,1,0] Output: false Explanation: The only way to decode it is two-bit character and two-bit character. So the last character is not one-bit character.
Constraints:
1 <= bits.length <= 1000bits[i] is either 0 or 1.Problem summary: We have two special characters: The first character can be represented by one bit 0. The second character can be represented by two bits (10 or 11). Given a binary array bits that ends with 0, return true if the last character must be a one-bit character.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array
[1,0,0]
[1,1,1,0]
gray-code)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #717: 1-bit and 2-bit Characters
class Solution {
public boolean isOneBitCharacter(int[] bits) {
int i = 0, n = bits.length;
while (i < n - 1) {
i += bits[i] + 1;
}
return i == n - 1;
}
}
// Accepted solution for LeetCode #717: 1-bit and 2-bit Characters
func isOneBitCharacter(bits []int) bool {
i, n := 0, len(bits)
for i < n-1 {
i += bits[i] + 1
}
return i == n-1
}
# Accepted solution for LeetCode #717: 1-bit and 2-bit Characters
class Solution:
def isOneBitCharacter(self, bits: List[int]) -> bool:
i, n = 0, len(bits)
while i < n - 1:
i += bits[i] + 1
return i == n - 1
// Accepted solution for LeetCode #717: 1-bit and 2-bit Characters
impl Solution {
pub fn is_one_bit_character(bits: Vec<i32>) -> bool {
let mut i = 0usize;
let n = bits.len();
while i < n - 1 {
i += (bits[i] + 1) as usize;
}
i == n - 1
}
}
// Accepted solution for LeetCode #717: 1-bit and 2-bit Characters
function isOneBitCharacter(bits: number[]): boolean {
let i = 0;
const n = bits.length;
while (i < n - 1) {
i += bits[i] + 1;
}
return i === n - 1;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.