Bit Patterns
What is this?
Sometimes what matters is not the value of a number but the shape made by its on and off switches โ like reading a barcode rather than a price tag. These problems look for arrangements such as switches that strictly alternate on-off-on-off, or treat the whole row of switches as a compact map of which seats in a cinema are taken.
๐ก Fun fact: Gray code, where each step flips just one switch, was patented by Bell Labs physicist Frank Gray in 1953 and is still used on rotary encoders so a sensor never misreads a value while spinning between positions.
๐ The 4 problems in this chapter are free. Sign in with Google or Microsoft to start solving.
Core idea: many problems are really about the shape of the bits, not their value. Shifting a number against itself and XOR-ing exposes where adjacent bits agree or differ, and treating a bitmask as a compact set lets you track occupancy or build sequences with one integer.
The pattern
The central move is shift-then-XOR. If you slide n right by one and XOR with itself, every position where two neighboring bits differ becomes a 1. For alternating bits, that result is all ones, which you confirm by checking (x & (x+1)) == 0. Gray code uses the same neighbor relationship the other way: i ^ (i >> 1) maps consecutive integers to codes that differ by exactly one bit, generating the whole sequence directly.
Reversing bits flips the layout end to end โ either swap symmetric positions one pair at a time, or use a divide-and-conquer cascade of masks that swaps halves, then quarters, then bytes in O(log w) steps. The seat-allocation problem treats a row as a bitmask of occupied seats: set a bit per booked seat, then AND against fixed window masks to test whether a contiguous block of seats is still free, updating the mask as families are placed.
The problems
- Binary Number with Alternating Bits โ shift-XOR to mark differing neighbors, then check the result is all ones.
- Gray Code โ generate the reflected sequence directly with
i ^ (i >> 1). - Reverse Bits โ swap symmetric bits, or cascade divide-and-conquer masks for
O(log w)reversal. - Cinema Seat Allocation โ keep a per-row bitmask of occupied seats and AND against window masks to find free contiguous blocks.
Key takeaways
- Shift-then-XOR reveals where adjacent bits differ โ the basis of the alternating-bits check.
i ^ (i >> 1)turns a counter into Gray code, where successive values differ by one bit.- Reverse bits by symmetric swaps or a log-step mask cascade rather than rebuilding the number digit by digit.
- A bitmask doubles as a compact set: one integer can track an entire row's occupancy.
- Precomputed window masks make "is this block free?" a single AND, turning layout questions into bit tests.
Core idea: Every cinema row is just ten seats, and only the middle eight (seats 2โ9) can ever hold a four-person family. Pack each row's reserved middle seats into a tiny bitmask, then test the three possible 4-seat blocks with a single bitwise AND each โ no scanning, no sorting beyond grouping reservations by row.
Problem, rephrased
You run seat allocation for a cinema. Each row has 10 seats numbered 1..10. A family of 4 insists on sitting together: four consecutive seats, and an aisle on each side of the row means they can only occupy one of three blocks:
- Left block: seats
2,3,4,5 - Middle block: seats
4,5,6,7 - Right block: seats
6,7,8,9
You're given n (number of rows) and reservedSeats, a list of [row, seat] pairs already booked. Return the maximum number of families you can seat across all rows.
The catch a senior eye spots immediately: seats 1 and 10 are useless to a family (no block uses them), and the left and right blocks don't overlap, so a single untouched row fits two families. The middle block exists only to rescue rows where the center is clear but a side is blocked.
| Row reservations (seats taken) | Seatable blocks | Families this row |
|---|---|---|
| (none) | left and right | 2 |
3 |
right only (6-9) |
1 |
5 |
right only (6-9) |
1 |
4,5,6,7 |
none (all blocks hit) | 0 |
1, 10 |
left and right (edges don't matter) | 2 |
The answer is the sum of families over all n rows.
Sign in to continue reading
The rest of this lesson is available with a free account. Signing in with Google or Microsoft is free.
Sign in to read the full lesson