~/problems

Problems

Basics first, then the classics, then company-style assessments. Every problem has tests you run right here; multi-level ones unlock as you go. See the roadmap.

Bit manipulation

Bit manipulation (IP / CIDR)

IPv4 as a 32-bit int; lowest set bit for block sizes.

Notes

Recognise it when: IP addresses and CIDR, flags, powers of two, subsets.

  • IPv4 as an int: n = (n << 8) | int(octet); back again: (n >> shift) & 0xFF.
  • Lowest set bit: x & -x, which is also the largest aligned block starting at x.
  • A /len block has size 1 << (32 - len), and its start must be a multiple of that size.
  • Is a power of two: x and not (x & (x - 1)). Set-bit count: x.bit_count().

Gotchas: at x == 0 everything is aligned, so cap the block at the whole space.

14 problems

Interview roadmap

Bit manipulation Masks, shifts and IP-address arithmetic.

esc