Explore math and geometry interview problems: rotate image, spiral matrix, fast powers, big-number multiplication and detect squares, animated step by step.
The pattern
Math & Geometry: a little insight, then careful bookkeeping
These problems don't share one algorithm. Each has a small mathematical idea (cells move in cycles of four, squaring halves an exponent, a square is fixed by its diagonal) and then needs careful index bookkeeping. The pictures show the bookkeeping step by step.
1Cells move in cycles
Rotating a square moves each corner to the next one: top → right → bottom → left. Four cells form a cycle, so one saved value is enough to rotate them in place.
2Halve the work
x^10 = (x^5)², and x^5 = (x^2)² × x. Squaring halves the exponent each time, so a power takes O(log n) multiplications instead of n.
3Arithmetic by hand, digit by digit
Numbers too long for the machine are lists of digits. Add or multiply them the way you would on paper, carrying from right to left.
Ideas worth remembering
- Matrix walks: four shrinking bounds (spiral) or rings from the outside in (rotation).
- In place: record first, change second (set zeroes), or rotate cycles with one saved value.
- Repeated operations: square-and-multiply for powers; a visited set or fast/slow pointers for sequences that might loop.
- Big numbers: digit lists and carries.
Reading the pictures
- Teal cells are being changed now; green ones are done; amber ones were just written to.
- Bigger interactive versions: Rotate Image and Spiral Matrix.
NeetCode 150 · Math & Geometry
All 8 problems
Pick a problem to see it solved step by step. They're in NeetCode's order, roughly easiest first.
Rotate Image
MediumBlind 75Rotate an n × n matrix 90° clockwise, in place (without a second matrix).
[[1,2,3], [4,5,6], [7,8,9]]
the matrix, rotated in place
variables
- layer
- –
Press Play or Step to watch the solution run.
Brute force vs the pattern
On this input:
- Pattern: O(n²)
- 8 ops
- Brute force
- n/a
Every cell has to move, so O(n²) is the best possible. Building a rotated copy (row r of the result is column r read bottom-up) is the easy way; the challenge is doing it in place. The answer is cross-checked against that copy.
“ops” is an illustrative count of selected algorithm operations, with estimates for sorting. It excludes drawing and frame creation and is not a timing benchmark. The complexity describes the Python solution. On small inputs a brute-force search may find the answer sooner.
How it works: Rotate Image
Step by step
- The four cells being rotated
- Current ring
Press Start or Step: the step the animation is on lights up below.
- 1
Rings from the outside in
l, r = 0, len(matrix) - 1 - 2
Rotate four cells
top_left = matrix[top][l + i]; matrix[top][l + i] = matrix[bottom - i][l]; …; matrix[top + i][r] = top_left - 3
All rings rotated
A 90° clockwise turn moves each cell of a ring to the next side: top → right → bottom → left → top. Four cells form a cycle, so you can move them with one temporary variable.
Walk the rings from the outside in (left and right pointers moving towards each other); in each ring, rotate the groups of four. (Another way: transpose the matrix, then reverse each row.)
Spot it in an interview
- In-place matrix transforms: find the cycles cells move along and rotate each cycle.
- Layer-by-layer processing of a square (also Spiral Matrix).
Each cell is moved once, using one temporary value.
Credits
Learn more from NeetCode
The problem list and order come from the NeetCode 150 by NeetCode (neetcode.io). The solutions here follow the approaches NeetCode teaches in the linked videos; the animations, explanations and code were written for CodeQuest. If these help, NeetCode's videos and site are well worth your time.
Problem statements are summarised in our own words; the full versions are on LeetCode.