</> 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

Fundamentals

What is this?

Think of a number as a row of light switches that are each either on or off. These pages teach you the basic vocabulary for working with those switches โ€” how to read one, turn it on, turn it off, or flip it โ€” before you tackle any puzzles. It is the alphabet you need before you can read the words.

flowchart TD A["See number as bit switches"] A --> B["Read a switch with AND"] B --> C["Turn on with OR, flip with XOR"] C --> D["Build a mask to target columns"] D --> E["Handle signs with twos complement"]

๐Ÿ’ก Fun fact: A single byte holds eight bits, which is why a classic color channel runs from 0 to 255 โ€” that is every possible combination of eight on/off switches, exactly 256 values.

๐Ÿ”“ The deep dives in this chapter are free. Sign in with Google or Microsoft to read the deep dives.


Core idea: every integer is just a fixed-width string of bits, and AND, OR, XOR, and the shifts let you read, set, clear, and move those bits one column at a time. Once you see numbers as bit vectors instead of magnitudes, a surprising amount of work collapses into a single machine instruction.

The pattern

These pages are concept primers, not problems โ€” they build the vocabulary the rest of the topic assumes.

  • The operators. AND keeps a bit only where both inputs have it (great for masking off what you don't want). OR keeps it where either does (turning bits on). XOR flips a bit where the inputs differ (toggling, and the engine behind self-canceling tricks). NOT inverts everything.
  • Shifts. x << k multiplies by 2^k and slides bits toward the high end; x >> k divides by 2^k (floor) and slides toward the low end. Shifting is how you build powers of two and reposition fields.
  • Masks. A mask is a hand-built bit pattern you combine with a value to isolate or alter specific columns: x & mask reads bits, x | mask sets them, x & ~mask clears them, x ^ mask flips them. 1 << i builds a single-bit mask for position i.
  • Two's complement. Signed 32-bit integers store negatives as the complement-plus-one, so the top bit is the sign and arithmetic "just works" without special cases. This is why -1 is all ones and why overflow wraps around instead of erroring.
  • Where it shows up. Permission flags and feature toggles packed into one int, RGBA color channels, network masks and IP subnetting, bitsets and Bloom filters, hardware register control, and compression. Anywhere memory or speed is tight, bits earn their keep.

The problems

This bucket holds tutorials rather than coding problems: Bit Manipulation Foundations, Essential Bit Tricks, 32-Bit Signed Representation, and Bit Manipulation Applications. Read them first โ€” the powers, XOR, and bit-pattern chapters all lean on the masks and two's-complement reasoning introduced here.

Key takeaways

  1. Treat an integer as a fixed-width bit vector; the operators act column by column.
  2. AND masks, OR sets, XOR toggles, NOT inverts โ€” pick the operator by what you want each bit to do.
  3. Left shift multiplies by powers of two, right shift divides; both reposition fields cheaply.
  4. Two's complement makes the sign bit and wraparound behavior fall out naturally on 32-bit ints.
  5. Bit tricks earn their place wherever space and speed matter โ€” flags, colors, networking, and compact data structures.

Bit Manipulation Applications

Core idea: Once you can treat an integer as a set of up to ~32โ€“64 elements, a whole class of problems gets a tiny, fast representation: a subset is one integer, "do these two collections overlap?" is one &, and "try every subset" is one for loop. This lesson surveys the patterns where that representation wins.


Why a bitmask beats a real set

For a small universe (โ‰ค ~26 letters, โ‰ค ~20 items), a Python set works but is heavy: hashing, pointers, allocation. A bitmask packs the same membership into a single machine word:

 letters present in "abz":   z y x ... c b a
                              1 0 0 ... 0 1 1   = (1<<25) | (1<<1) | (1<<0)
  • Membership: mask >> i & 1
  • Union / intersection / difference: a | b, a & b, a & ~b
  • Disjoint? a & b == 0
  • Size: mask.bit_count()

One & replaces a nested membership loop โ€” that's the recurring win below.


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.