Skip to content

Volume ITechnology

4.28.0 — Math & Geometry: The Pattern ​

Recognition cue. There is no data structure to reach for. The problem is arithmetic, index manipulation on a grid, or a small piece of geometry — and the answer is a formula or a careful loop rather than a map or a heap.

These problems are not hard. They are fiddly. The failures come from off-by-one errors and untested edge cases, not from missing an algorithm. So the method is different: derive the formula on a tiny example before writing any code.

The recurring moves ​

Digit walking. divmod(n, 10) peels the last digit and shifts the rest down. It appears in Happy Number, Plus One, Reverse Integer and Multiply Strings.

Index formulas on a grid. Deriving them once and checking them on a corner is the whole technique.

you wantformula
flatten (r, c) into one indexr * cols + c
unflatten index i(i // cols, i % cols)
the 3×3 box of a Sudoku cell(r // 3) * 3 + (c // 3)
the two diagonals through (r, c)r − c and r + c
digit i × digit j lands ati + j and i + j + 1

Matrix transformations, all built from transpose and reverse:

operationrecipe
rotate 90° clockwisetranspose, reverse each row
rotate 90° anticlockwisetranspose, reverse each column
rotate 180°reverse the row order and each row
mirror horizontallyreverse each row

Halving the exponent. Fast exponentiation reads the exponent in binary and squares repeatedly, giving O(\log n).

Exact comparisons only. Compare squared distances rather than distances, and reduced fractions rather than slopes. Floating point turns a correct algorithm into an unreliable one.

The eight problems ​

#ProblemThe one insight
4.28.1Rotate ImageTranspose, then reverse each row
4.28.2Spiral MatrixFour boundaries, and two guards for the last row or column
4.28.3Set Matrix ZeroesUse row 0 and column 0 as the record; [0][0] collides
4.28.4Happy NumberA deterministic process is a linked list — use cycle detection
4.28.5Plus OneThe early return replaces the carry variable
4.28.6Pow(x, n)Read the exponent in binary; square repeatedly
4.28.7Multiply StringsDigits i and j land at i+j and i+j+1
4.28.8Detect SquaresThe diagonal determines the other two corners

The number theory behind several of these, and behind every "return the answer modulo 10⁹ + 7" in this Part, is taught on its own page: 4.28.9 GCD, Primes & Modular Arithmetic.

The traps on this pattern ​

Untested boundary cases. A 1×n matrix in Spiral Matrix, all nines in Plus One, the most negative integer in Pow. Test the degenerate input before saying you are done.

Overflow. Negating the most negative 32-bit integer does not fit. Python is immune; most languages are not, and interviewers ask.

Floating point where integers would do. Squared distances, reduced fractions, and integer ceiling division (a + b - 1) // b.

Converting a huge digit string to an integer. It works in Python and overflows everywhere else — which is exactly why Multiply Strings and Plus One forbid it.

Swapping every pair twice. In a transpose, j must start at i + 1.

What the interviewer will push on ​

"Derive the index formula." Do it on a 2×2 or a corner cell, out loud.

"What is the degenerate input?" Name it before they do.

"Does this overflow?" Know where your language protects you and where it does not.

"Why not use floating point?" Precision. Give the squared-distance example.

One thing to volunteer: check your formula on the smallest example before writing the loop. Every problem in this group is one formula plus careful boundaries, and ten seconds of checking removes the only real risk.

Recall

  • These problems are fiddly, not hard — derive the formula on a tiny example first.
  • Digit walking: divmod(n, 10) peels the last digit.
  • Grid formulas: flatten with r * cols + c; the Sudoku box is (r//3)*3 + (c//3); the diagonals are r − c and r + c.
  • Rotate 90° clockwise = transpose then reverse each row. In the transpose, j starts at i + 1 or every pair is swapped twice.
  • Fast exponentiation reads the exponent in binary: square each step, multiply in when the bit is set. O(\log n).
  • Multiply Strings: digits i and j contribute to positions i+j and i+j+1, and the result has at most m + n digits.
  • Compare exact quantities — squared distances, reduced fractions — never floats.
  • Watch the degenerate input: a single row, all nines, the most negative integer.

Next: 4.28.1 Rotate Image — two simple operations replacing one formula that is easy to get wrong.

Combine chapters into one PDF

Pick the chapters you want. They print in the order you pick them.

Volume I — Technology
Part 0 — Retention & Notation
Part 1 — Foundations of Computation
Part 2 — Operating Systems
2.1 The Kernel
2.2 Processes
2.3 Threads & Scheduling
2.4 Concurrency Under the Hood
2.5 Memory Management
2.6 File Systems
2.7 Input/Output
2.8 Linux for Engineers
2.9 Windows & macOS in Practice
Part 3 — Languages, Compilers & Runtimes
3.6 JavaScript & V8
3.7 TypeScript
3.8 Node.js Internals
3.13 Python & CPython
3.14 FastAPI & Production Python
Part 4 — Data Structures & Algorithms
4.0 Classic Problems — Before NeetCode
4.4 Arrays & Hashing — Problems
4.5 Two Pointers
4.6 Sliding Window
4.8 Stack — Problems
4.9 Linked List — Problems
4.11 Binary Search — Problems
4.13 Trees
4.14 Tree — Problems
4.15 Trie — Problems
4.17 Heap — Problems
4.18 Backtracking — Problems
4.19 Graphs
4.20 Graph — Problems
4.21 Advanced Graph — Problems
4.23 1-D DP — Problems
4.24 2-D DP — Problems
4.26 Greedy — Problems
4.27 Interval — Problems
4.28 Math & Geometry — Problems
4.30 Bit Manipulation — Problems
Part 5 — Networking & the Web
5.3 IP
5.4 Transport
5.6 HTTP
Part 6 — Frontend Engineering
6.1 Browser Internals
6.2 HTML & CSS Mastery
6.3 JavaScript in the Browser
6.4 React, from First Component to Full Shop
6.5 Frameworks & Rendering
6.8 UI Engineering & Patterns
Part 7 — Databases & Storage
7.2 SQL Mastery
7.3 Storage Engine Internals
7.4 Transactions
7.5 NoSQL Taxonomy
7.8 Analytics & Big Data
Part 8 — Security, Identity & Cryptography
8.2 Cryptography
8.3 Protocols of Trust
8.4 Identity: AuthN & AuthZ
8.5 Application Security
8.6 Infrastructure Security
Part 9 — Software Design (LLD)
9.2 OOP Foundations
9.3 Design Principles
9.4 Design Patterns
9.5 Concurrency
9.6 API Design
9.7 LLD Interview Machines
9.9 Node/Express Production Playbook
Part 10 — Distributed Systems & HLD
10.7 Consistency & Consensus
10.8 Messaging & Microservices
10.14 Caching
Building blocks from the design seat
Part 11 — System Design Case Studies
Part 12 — AI & ML Engineering
12.1 What AI Engineering Is
12.2 The Math You Need
12.3 Classical Machine Learning
12.4 Data for ML
12.5 Deep Learning
12.6 Embeddings & Vector Search
12.7 Transformers & LLMs
12.8 Building with LLMs
12.9 Retrieval-Augmented Generation
12.10 Agents
12.11 Adapting Models
12.12 Production AI
12.13 Multimodal AI
12.14 AI Security & Governance
12.15 Case Studies & Capstone
Part 13 — Cloud, DevOps & Infrastructure
13.11 Git, from First Commit to Team Workflow
Part 14 — Containers: Docker & Kubernetes
14.1 Foundations
14.2 Docker
14.3 Kubernetes
14.4 Kubernetes on Azure
Part 15 — The Craft & Career
Nothing picked yet