SDE & MLE Prep

Math & Geometry

Matrix tricks

  • Rotate 90° clockwise = transpose then reverse each row.
  • Spiral = shrink four boundaries top, bottom, left, right.
  • O(1)-space marking: reuse the first row/column as flags.

Number tricks

  • Fast power: square-and-multiply on the bits of the exponent → O(log n).
  • Cycle detection on sequences (happy number) = Floyd, like linked lists.
  • Sieve of Eratosthenes for all primes < n in O(n log log n).
  • Geometry: use integer arithmetic — represent slopes as reduced (dy, dx) pairs, never floats.
rotate:  1 2 3     transpose  1 4 7    reverse rows  7 4 1
         4 5 6    ---------> 2 5 8   ------------>  8 5 2
         7 8 9                3 6 9                  9 6 3

Problems