Skip to content

Volume ITechnology

4.0 — How to Recognise a Problem in Thirty Seconds ​

Strong problem-solvers are not faster thinkers. They are people who have seen the shape before, so the first thirty seconds of reading produces a template rather than a blank page.

Part 4 builds that skill in one repeating rhythm. A chapter teaches a structure or a technique. The chapter straight after it works every problem that uses it, one problem per page. The problems come from the NeetCode 150, a widely used list of 150 interview problems. So hashing is Chapter 4.3 and its nine problems are Chapter 4.4. Heaps are 4.16 and their seven problems are 4.17. Dynamic programming is 4.22 and its twenty-three problems are 4.23 and 4.24. Read straight through in order and nothing ever appears before the thing it is built from.

If you are new to all of this, read this page as a map of what is coming. The table below names many things you have not met yet: hash maps, tries, heaps, dynamic programming. You are not expected to know them here. Each one is taught from the beginning in the chapter listed beside it. Skim this page now. Then come back to it after each chapter, and one more row of the table will make sense each time.

Every problem page is laid out the same way, because the layout is the method:

  1. The problem, said plainly.
  2. What you already know that solves it.
  3. The brute force, and exactly what is wasteful about it.
  4. The idea, in one sentence.
  5. The code, with every line explained, and the thinking behind it.
  6. The complexity, with the tricky part worked out.
  7. The edge cases.
  8. The general lesson: which other problems the same move solves.

1. The recognition table ​

This is the page to come back to. Left column is what you read in the problem; right column is what to reach for.

What the problem saysPatternPage
"have I seen this", "duplicate", "count of each"hash map / set4.4.0
"group by some property"hash on a fingerprint4.4.0
"sum of a range", asked many timesprefix sum4.4.0
sorted array, find a pair / tripletwo pointers from both ends4.5.0
"in place", "remove", "partition"write pointer4.5.0
"palindrome"two pointers inward4.5.0
contiguous subarray or substring, "longest/shortest/at most K"sliding window4.6.0
"valid parentheses", "nesting", "undo"stack4.8.0
"next greater / smaller element", "span"monotonic stack4.8.0
sorted input, or "minimise the maximum"binary search, incl. on the answer4.11.0
"O(\log n) required"binary search4.11.0
linked list, "reverse / middle / cycle"slow-fast pointers, dummy head4.9.0
tree, "path / depth / subtree"DFS, usually post-order4.14.0
tree, "level", "shortest"BFS with a queue4.14.0
"prefix", "starts with", "dictionary"trie4.15.0
"top k", "k-th largest", "median of a stream"heap of size k4.17.0
"merge k sorted things"heap4.17.0
"all combinations / permutations / subsets", "generate every"backtracking4.18.0
grid of cells, "islands / regions / flood"BFS or DFS over the grid4.20.0
"prerequisites", "ordering", "dependency"topological sort4.20.0
"shortest path" with weightsDijkstra4.21.0
"connect everything cheaply"MST — Kruskal with union-find4.21.0
"are these connected", edges arrivingunion-find4.21.0
"how many ways", "maximum/minimum over choices"DP4.23.0 · 4.24.0
two strings compared2-D DP grid4.24.0
"maximum profit / fewest coins", locally obvious best movegreedy — then try to break it4.26.0
pairs of (start, end)intervals, sort first4.27.0
"without division", "in O(1) space", digitsmath tricks4.28.0
"appears once", "no extra memory", "count bits"XOR and bit tricks4.30.0

2. The five questions to ask before writing anything ​

Ask these in order, every time. They take under a minute. They prevent the two failures that most often cost people a job offer. The first is solving the wrong problem. The second is writing a solution that is too slow for the size of the input.

A word on the notation first. O(n^2) is read "order n squared". It means that when the input has n items, the work grows like n × n. O(n \log n) grows much more slowly. Chapter 4.1 teaches this notation from zero, so if it is new to you, the tables below will make full sense after that chapter.

1. What am I given, and what is the size? Every problem states its limits, such as "n is at most 100,000". These limits are called the constraints, and they are a hint. The table below maps the input size to the complexity the problem setter has in mind. It assumes a computer does roughly 10^8 simple operations per second. Memorise it.

n up toIntended complexity
10–12O(n!) — permutations
20–25O(2^n) — subsets, bitmask DP
100–500O(n^3) — interval DP, Floyd-Warshall
1,000–5,000O(n^2) — 2-D DP over both indices
10^5–10^6O(n \log n) — sort, heap, binary search
10^7+O(n) or O(\log n) — one pass, hashing, two pointers

"n up to 20" is the setter telling you an exponential answer is expected. "n up to 10^6" is them telling you that sorting is the most you can afford.

2. What is the brute force, and what is its complexity? Always say it out loud. It proves you understand the problem, it gives you a correct fallback, and — most importantly — it tells you what to attack. "Brute force is O(n^2) because for every element I scan every other element to check if I have seen it" contains its own fix: the scan is the waste, so hash it.

3. What is being recomputed? Almost every optimisation is one of four moves.

  • Recomputing a lookup → hash map.
  • Recomputing a sum or count over a window → sliding window or prefix sum.
  • Recomputing a subproblem → memoisation (Chapter 4.22).
  • Recomputing a comparison against everything → sort first, then two pointers or binary search.

4. Is there structure I am ignoring? Sorted input means binary search or two pointers. Non-negative values mean a sliding window can shrink safely. A bounded alphabet means a fixed-size array instead of a map. "Exactly one solution exists" means you can return early. The problem statement rarely wastes words.

5. What are the edge cases? Empty input, one element, all identical, negative numbers, integer overflow, and duplicates. Name them before coding, handle them in the code, and mention them when you finish.

3. The moves that appear everywhere ​

Nine ideas account for most of the 150 problems. Each one is built step by step in Chapters 4.1–4.29. On a first read, treat this list as a preview. When you return after those chapters, it becomes a checklist of what to look for when a problem is trying to hide the idea.

Trade space for time. The single most common optimisation. A nested loop asking "does this exist elsewhere" becomes one loop plus a hash set. O(n^2) to O(n) at the cost of O(n) memory.

Sort to create structure. Sorting costs O(n \log n) and buys you two pointers, binary search, greedy ordering and adjacency of equal elements. If the brute force is O(n^2) and sorting does not destroy what you need, sorting is usually the first thing to try.

Two pointers instead of two loops. When you can prove that moving one pointer never needs to be undone, you replace O(n^2) with O(n). The proof is always the same shape: this element cannot be part of any better answer, so discarding it is safe.

A window with a shrink rule. For contiguous ranges. Expand the right edge always, shrink the left edge while a condition is violated. Both pointers only move forward, so it is O(n) even though it looks nested — the amortized argument from Chapter 4.1.

Compute once, answer many times. Prefix sums, precomputed hashes, a preprocessed table. Pay O(n) up front, answer each query in O(1).

Remember what you have already solved. Memoisation. You can spot it when the recursion tree has the same node in several places.

Keep only what can still win. The monotonic stack and monotonic deque discard elements that are provably useless. Each element enters and leaves once, so the whole thing is linear.

Search the answer, not the data. When the answer is a number and "is X achievable" is easier than "what is the best X", binary search the answer space. Chapter 4.10 calls this searching a monotonic predicate, which means a yes/no question whose answer switches from "no" to "yes" once and never switches back.

Reverse the direction. Working backwards from the target, iterating from the right, or reversing the graph's edges. Several problems that are painful forwards become trivial backwards.

4. How to use these pages ​

Before NeetCode: the classic problems. Start-ups, service companies and college exams open with a different set of questions. Swap two variables without a third, reverse a number, check for a prime, find the HCF, print a pyramid, find the second largest, and write bubble, merge and quick sort by hand. These are collected in 4.0.0–4.0.32, in Python only. The easy ones are short. The ones that need thought are walked through line by line. If you are new to problem solving, do them first. Every NeetCode pattern later builds on the moves they teach.

Each problem group opens with a .0 page — the pattern itself. That page carries four things: the recognition cue (the exact phrases in a problem statement that mean this pattern), the annotated template with the line that changes between problems marked, the traps people fall into on this pattern specifically, and what the interviewer will push on. Then one page per problem, in the order NeetCode lists them, which is roughly easiest first.

The Blind 75 subset is flagged with a ★. If you are short on time, do the starred ones across every group rather than all of one group — coverage of shapes beats depth in one shape.

The code is Python first, TypeScript second. Python because that is what most people type into the judge and what the interviewer will least often argue with; TypeScript in a second tab because the rest of Volume I is written in it and because a few of these problems teach something about JavaScript specifically. Where the two languages genuinely disagree — integer division, how a tuple can be a dictionary key, what [[]] * n does to you — the page says so at the point where it would bite.

A note on how to practise. Reading a solution and understanding it produces almost no retention. What produces retention is: read the problem, say the pattern out loud before scrolling, then check. If you were right, move on — you do not need to write the code. If you were wrong, that is the problem worth writing out in full. The recognition is the skill; the code is the easy part once you have it.

Next: if you are new to problem solving, go to the classic problems first, starting at 4.0.0. Otherwise go straight to 4.1. It teaches you how to count the cost of a piece of code, and how to tell in advance which complexity the problem setter is asking for.

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