</> MAANG.io
coding interview ยท 101

Foundations

Master coding interviews with comprehensive coverage of data structures, algorithms, and problem-solving techniques. Progress from fundamentals to advanced topics with expertly curated content.

0/255 solved 0% complete

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.

flowchart TD A["Focus on the shape of the switches"] A --> B["Shift then XOR to find differing neighbors"] B --> C["Check for an alternating layout"] C --> D["Generate Gray code with i XOR shifted i"] D --> E["Use a mask as a compact seat map"]

๐Ÿ’ก 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

  1. Shift-then-XOR reveals where adjacent bits differ โ€” the basis of the alternating-bits check.
  2. i ^ (i >> 1) turns a counter into Gray code, where successive values differ by one bit.
  3. Reverse bits by symmetric swaps or a log-step mask cascade rather than rebuilding the number digit by digit.
  4. A bitmask doubles as a compact set: one integer can track an entire row's occupancy.
  5. Precomputed window masks make "is this block free?" a single AND, turning layout questions into bit tests.

Core idea: Reversing bits is just mirroring them around the center โ€” do it one bit at a time, or fold the word in half, then in quarters, then eighths, until every bit has crossed to its mirror seat.

Problem, rephrased

You're handed a fixed-width 32-bit unsigned integer. Picture its bits laid out left-to-right, position 31 down to position 0. Your job: produce a new 32-bit value where the bit at position 0 lands at position 31, position 1 at position 30, and so on โ€” a perfect end-for-end mirror.

It's the bit-level equivalent of reversing a string, except the "string" is always exactly 32 characters of 0/1 and you must keep it that width.

Input (decimal) Input (32-bit binary) Output (32-bit binary) Output (decimal)
43261596 00000010100101000001111010011100 00111001011110000010100101000000 964176192
1 00000000000000000000000000000001 10000000000000000000000000000000 2147483648
4294967295 11111111111111111111111111111111 11111111111111111111111111111111 4294967295

Notice the all-ones case is its own reverse โ€” a palindrome. Most inputs are not.

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

Sign in to MAANG.io

Use your Google or Microsoft account โ€” no password to remember.

Continue with Google Continue with Microsoft

Please accept the terms above to continue.