All questions
Showing of 75How does binary search locate a value in a sorted array so quickly?
Answer it yourself first - out loud, or typed below.
How should your speech become text?
Listening… your words appear above as you speak - tap Stop when you're done.
Recording · cr - tap Stop & transcribe when you're done.
Transcribing with AI…
Voice:
Last attempt -
Binary search checks the middle element, then throws away half the array on every step. If the middle is too small, the value must sit to the right, so you ignore the left half. If it is too big, you ignore the right. You repeat on the surviving half until you land on the value or run out.
That halving is why it feels instant. A million-element array needs about twenty checks, not a million. The cost is O(log n) comparisons and O(1) extra space.
The catch is that the array must already be sorted on the key you search. On unsorted data the halving logic breaks, since a small middle no longer tells you which side to drop. This is why people sort once and then search many times.
This answer doesn't lend itself to a diagram - it reads best . No credits were charged.
Why there's no diagram: “”
The interactive diagram is below the answer - jump to diagram ↓ · Below it, the related concept . Jump to it ↓
How does merge sort split a list apart and merge it back sorted?
Answer it yourself first - out loud, or typed below.
How should your speech become text?
Listening… your words appear above as you speak - tap Stop when you're done.
Recording · cr - tap Stop & transcribe when you're done.
Transcribing with AI…
Voice:
Last attempt -
Merge sort keeps splitting the list in half until each piece holds one element. A single element is already sorted by itself. Then it merges pairs back together, walking two sorted pieces and picking the smaller front item each time.
Merging two sorted halves is cheap because both are ordered. You compare their fronts, take the winner, and advance. You repeat until both are empty and one sorted run remains.
The splitting gives log n levels, and each level touches every element once, so total work is O(n log n). It needs O(n) extra room to hold the merged output. That steady cost, with no bad cases, is why merge sort is a safe default when memory is not tight.
This answer doesn't lend itself to a diagram - it reads best . No credits were charged.
Why there's no diagram: “”
The interactive diagram is below the answer - jump to diagram ↓ · Below it, the related concept . Jump to it ↓
How does quicksort partition a list around a pivot to sort in place?
Answer it yourself first - out loud, or typed below.
How should your speech become text?
Listening… your words appear above as you speak - tap Stop when you're done.
Recording · cr - tap Stop & transcribe when you're done.
Transcribing with AI…
Voice:
Last attempt -
Quicksort picks one element as a pivot, then rearranges the array so smaller items sit left of it and larger items sit right. After that single pass, the pivot is in its final sorted spot. You then sort the left and right chunks the same way.
The rearranging happens in place. You walk the array with a pointer, swapping any item smaller than the pivot toward the front. When you finish, you swap the pivot into the boundary you built.
Because it moves items within the same array, quicksort needs almost no extra memory, only O(log n) for recursion. Average speed is O(n log n). The in-place swaps and tight loops make it fast in practice, which is why many libraries lean on it.
This answer doesn't lend itself to a diagram - it reads best . No credits were charged.
Why there's no diagram: “”
The interactive diagram is below the answer - jump to diagram ↓ · Below it, the related concept . Jump to it ↓
What are the practical differences between quicksort and merge sort?
Answer it yourself first - out loud, or typed below.
How should your speech become text?
Listening… your words appear above as you speak - tap Stop when you're done.
Recording · cr - tap Stop & transcribe when you're done.
Transcribing with AI…
Voice:
Last attempt -
Quicksort sorts in place and uses only O(log n) extra memory, while merge sort needs O(n) scratch space to hold merged runs. On large arrays that memory gap matters.
Speed differs in the tails. Both average O(n log n), but quicksort can drop to O(n^2) on bad pivots, whereas merge sort holds O(n log n) always. Merge sort is also stable, keeping equal items in original order; plain quicksort is not.
So the choice follows your data:
- Pick quicksort for raw speed and tight memory on in-memory arrays.
- Pick merge sort when you need stability, guaranteed timing, or you sort linked lists or external files.
This split is why array libraries often tune quicksort while stable or external sorts reach for merging.
This answer doesn't lend itself to a diagram - it reads best . No credits were charged.
Why there's no diagram: “”
The interactive diagram is below the answer - jump to diagram ↓ · Below it, the related concept . Jump to it ↓
What does it mean for a sorting algorithm to be stable?
Answer it yourself first - out loud, or typed below.
How should your speech become text?
Listening… your words appear above as you speak - tap Stop when you're done.
Recording · cr - tap Stop & transcribe when you're done.
Transcribing with AI…
Voice:
Last attempt -
A stable sort keeps equal items in the same order they had before sorting. If two records share a sort key, the one that started first still comes first afterward.
This matters when you sort by more than one field. Say you sort people by first name, then by last name. A stable sort preserves the first-name order within each shared last name, so you get a clean two-level ordering for free.
With an unstable sort, equal keys can swap around unpredictably. You lose that earlier arrangement, and multi-pass sorting stops working. Merge sort and insertion sort are naturally stable; plain quicksort and heapsort are not. Knowing which one you have decides whether you can chain sorts to build compound orderings.
This answer doesn't lend itself to a diagram - it reads best . No credits were charged.
Why there's no diagram: “”
The interactive diagram is below the answer - jump to diagram ↓ · Below it, the related concept . Jump to it ↓
Which sorting algorithm does your language's standard library actually use?
Answer it yourself first - out loud, or typed below.
How should your speech become text?
Listening… your words appear above as you speak - tap Stop when you're done.
Recording · cr - tap Stop & transcribe when you're done.
Transcribing with AI…
Voice:
Last attempt -
Most mature languages do not ship a single textbook sort. They ship a hybrid tuned for real data. Python and Java's object sort use Timsort, which blends merge sort with insertion sort and exploits runs that are already ordered.
C++ std::sort uses introsort, which starts as quicksort and switches to heapsort if recursion goes too deep. That switch guards against quicksort's O(n^2) worst case while keeping its usual speed.
Two patterns show up everywhere. Sorts of objects tend to be stable, often Timsort. Sorts of raw numbers tend to be unstable and in place, often an introsort variant.
Knowing your library's choice tells you its stability and its worst-case guarantee, so you never reimplement a sort the runtime already does well.
This answer doesn't lend itself to a diagram - it reads best . No credits were charged.
Why there's no diagram: “”
The interactive diagram is below the answer - jump to diagram ↓ · Below it, the related concept . Jump to it ↓
What is recursion, and what two ingredients does every recursive function need?
Answer it yourself first - out loud, or typed below.
How should your speech become text?
Listening… your words appear above as you speak - tap Stop when you're done.
Recording · cr - tap Stop & transcribe when you're done.
Transcribing with AI…
Voice:
Last attempt -
Recursion is when a function solves a problem by calling itself on a smaller version of the same problem. Each call handles one slice, then hands the rest to another call.
Every recursive function needs two ingredients. First, a base case: an input small enough to answer directly, with no further call. Second, a recursive step that shrinks the problem and calls itself, moving steadily toward that base case.
Miss either one and it breaks. Without a base case the calls never stop. Without real progress toward it, you also never stop.
A factorial shows both parts clearly:
function fact(n) {
if (n <= 1) return 1; // base case
return n * fact(n - 1); // shrinks toward base
}
// fact(4) === 24
This answer doesn't lend itself to a diagram - it reads best . No credits were charged.
Why there's no diagram: “”
The interactive diagram is below the answer - jump to diagram ↓ · Below it, the related concept . Jump to it ↓
What is a base case, and what happens when a recursion lacks one?
Answer it yourself first - out loud, or typed below.
How should your speech become text?
Listening… your words appear above as you speak - tap Stop when you're done.
Recording · cr - tap Stop & transcribe when you're done.
Transcribing with AI…
Voice:
Last attempt -
A base case is the stopping condition that returns an answer directly, without another recursive call. It is the point where the problem is small enough to solve outright, like an empty list or zero.
Without one, the function keeps calling itself forever. The problem never shrinks to something answerable, so the recursion has no exit.
That runaway is not harmless. Each call reserves a frame on the call stack for its local state. Endless calls pile up frames until the stack runs out of room, and the program crashes with a stack overflow.
A wrong base case bites the same way. If your condition never quite matches the values you reach, you skip the exit and fall into the same endless descent. Getting the base case exact is the first thing to check.
This answer doesn't lend itself to a diagram - it reads best . No credits were charged.
Why there's no diagram: “”
The interactive diagram is below the answer - jump to diagram ↓ · Below it, the related concept . Jump to it ↓
What is backtracking, and how does it differ from plain brute-force search?
Answer it yourself first - out loud, or typed below.
How should your speech become text?
Listening… your words appear above as you speak - tap Stop when you're done.
Recording · cr - tap Stop & transcribe when you're done.
Transcribing with AI…
Voice:
Last attempt -
Backtracking builds a solution one choice at a time and abandons a path the moment it cannot possibly work. It explores a tree of partial choices, going deeper while things look valid and retreating when they do not.
Plain brute force is blunter. It generates every full candidate, then checks each one at the end. It never notices a doomed path early, so it wastes effort completing arrangements that were broken from the start.
The difference is when you check. Backtracking tests constraints during construction and prunes dead branches before finishing them. Brute force tests only after building a whole candidate.
That early pruning is the whole payoff. On problems like placing queens or filling a grid, backtracking skips huge regions of hopeless combinations, while brute force grinds through all of them.
This answer doesn't lend itself to a diagram - it reads best . No credits were charged.
Why there's no diagram: “”
The interactive diagram is below the answer - jump to diagram ↓ · Below it, the related concept . Jump to it ↓
What is dynamic programming, and what kind of problem does it solve?
Answer it yourself first - out loud, or typed below.
How should your speech become text?
Listening… your words appear above as you speak - tap Stop when you're done.
Recording · cr - tap Stop & transcribe when you're done.
Transcribing with AI…
Voice:
Last attempt -
Dynamic programming solves a big problem by combining answers to smaller overlapping subproblems, and it stores each small answer so it is computed only once. That reuse is the whole point.
It fits problems with two traits. First, optimal substructure: the best answer is built from best answers to smaller pieces. Second, overlapping subproblems: the same smaller pieces show up again and again during a plain recursive solve.
When both hold, naive recursion redoes the same work exponentially. Dynamic programming caches each subproblem result, turning that blow-up into something like O(n) or O(n*m) time, traded for memory to hold the table.
Typical fits are counting paths, shortest routes, and optimal splits. If a recursive solution keeps recomputing identical calls, that repetition is the signal to reach for it.
This answer doesn't lend itself to a diagram - it reads best . No credits were charged.
Why there's no diagram: “”
The interactive diagram is below the answer - jump to diagram ↓ · Below it, the related concept . Jump to it ↓
What is a greedy algorithm, and when does the greedy choice work?
Answer it yourself first - out loud, or typed below.
How should your speech become text?
Listening… your words appear above as you speak - tap Stop when you're done.
Recording · cr - tap Stop & transcribe when you're done.
Transcribing with AI…
Voice:
Last attempt -
A greedy algorithm builds an answer step by step, always grabbing the option that looks best right now. It never revisits a past choice. This makes it fast and simple, usually O(n log n) after a sort, sometimes O(n).
The greedy choice works when two things hold. First, a locally best pick is part of some overall best answer (the greedy-choice property). Second, solving what remains after that pick gives the full solution (optimal substructure).
Making change with coin sizes like 1, 5, 10, 25 works greedily: take the biggest coin that fits, repeat. It fails on odd coin sets, where a smaller first coin wins.
When you cannot prove the pick is safe, greedy quietly returns a wrong answer. Test it against a slower exact method before trusting it.
This answer doesn't lend itself to a diagram - it reads best . No credits were charged.
Why there's no diagram: “”
The interactive diagram is below the answer - jump to diagram ↓ · Below it, the related concept . Jump to it ↓
What must be true about your data before you can use binary search?
What are the time and space costs of the common sorting algorithms?
How does the call stack keep track of nested recursive calls?
Why can deep recursion crash a program with a stack overflow?
When is recursion clearer or simpler than an equivalent loop?
What is memoization, and how does caching results speed up recursion?
What are overlapping subproblems, and why do they slow plain recursion?
When does quicksort degrade to quadratic time, and what triggers it?
How does the choice of pivot change quicksort's behavior and cost?
Why is quicksort often faster than merge sort in real practice?
How does heapsort work, and why is it rarely the default choice?
When does counting sort beat every comparison-based sorting algorithm?
How does radix sort work, and what kind of data suits it?
Why can no comparison-based sort ever run faster than n log n?
How do you run a binary search over an answer space?
How do you find the first or last match with binary search?
What is the classic overflow bug when computing a binary search midpoint?
Which common sorts are stable, and why does stability cost something?
When and how do you rewrite a recursive function as a loop?
What is tail recursion, and when can it avoid growing the stack?
How do you work out the time complexity of a recursive function?
How does the Master Theorem analyze divide-and-conquer running times?
What general template do most backtracking problems share?
How does pruning make backtracking practical on a large search space?
Why does backtracking so often have exponential worst-case running time?
How does backtracking handle permutations differently from combinations?
How do you stop a backtracking search from producing duplicate solutions?
How do you recognize that a problem is a good fit for dynamic programming?
What is the difference between memoization and tabulation?
How do you define the state and transition for a dynamic programming solution?
When is a greedy algorithm provably correct, and how do you show it?
Why can a greedy choice give a wrong answer, and how do you spot it?
What is optimal substructure, and how does it differ from the greedy-choice property?
How do you cut a DP's memory by keeping only the rows you need?
How would you sort far more data than fits in memory?
How do you sort a huge dataset spread across many machines?
How do cache effects make some n log n sorts faster than others?
How does a hybrid sort like Timsort combine merging and insertion sort?
How does introsort protect quicksort from its quadratic worst case?
How do you make an in-place sort stable, and what does it cost?
How do you sort efficiently when each comparison is very expensive?
How do you parallelize quicksort or merge sort across threads safely?
What breaks in binary search when the array holds billions of elements?
How do you binary search a monotonic function you can only sample?
When do you use quickselect instead of sorting to find the k-th element?
How do you rescue a recursion that overflows the stack on real inputs?
How do you analyze a recursive algorithm's space cost, not just its time?
How does tail-call optimization differ across languages, and which ones lack it?
How do you parallelize a divide-and-conquer algorithm effectively?
How do you bound a backtracking search that could explore billions of states?
How does branch-and-bound improve on plain backtracking search?
How does ordering your choices help backtracking find solutions sooner?
How do you detect and cut symmetric branches in a backtracking search?
When does iterative deepening beat a plain depth-first backtracking search?
How does constraint propagation speed up a backtracking solver?
How do you recover the actual solution, not just its cost, from a DP table?
How do you reconstruct a DP answer while using only linear memory?
How does bitmask DP encode subsets, and where does it stop scaling?
When is a greedy heuristic an acceptable approximation for a hard problem?
How do recursion depth and caching push a large DP toward tabulation?
How do memory-access patterns affect a large DP table's real-world speed?
How do you parallelize a dynamic program given its dependency structure?
What can you do when a DP's state space is too large to enumerate?
How do you choose between greedy, divide-and-conquer, and dynamic programming for a new problem?
This answer is part of Pro.
The full written answer, with the trade-offs and follow-ups an interviewer will probe.
No matches
Try a different filter or search term.
Sorting, Searching & Recursion cheatsheet
- 30-second mental model01
- Picking a sort02
- Binary search03
- Recursion04
- Backtracking05
- Dynamic programming06
- Common pitfalls07
- + 1 more inside
64 of 75 Sorting, Searching & Recursion answers are in Pro.
Full answers, code samples, and AI explanations that go simpler or deeper. Cancel anytime.
- Full answers + code
- AI explanations, simpler or deeper
- 1,000 AI credits / month
- Cancel anytime
Change topic
Pick a different technology or stack. Your current topic stays put until you choose a new one.
MEAN
MongoDB, Express, Angular, Node.jsMERN
MongoDB, Express, React, Node.jsDjango
Python Full-Stack DevelopmentRuby on Rails
Convention over ConfigurationServerless on AWS
Serverless Architecture on AWSInterviewers also test these - they're common to every stack, whichever one you picked above.
Flutter Mobile
Flutter Cross-Platform Mobile DevelopmentInterviewers also test these - they're common to every stack, whichever one you picked above.
Spring Boot
Enterprise Java Development.NET
Microsoft EcosystemVue
Vue.js, Vite, TypeScript, Tailwind, Node.jsGo Backend
Golang, gRPC, PostgreSQL, Redis, RabbitMQInterviewers also test these - they're common to every stack, whichever one you picked above.
FastAPI
Python, FastAPI, SQLAlchemy, PostgreSQLReact Native
React, TypeScript, Redux, FirebaseiOS Native
Swift, SwiftUI, UIKit, FirebaseAndroid Native
Java, Jetpack Compose, FirebaseDevOps / Platform
Docker, Kubernetes, Terraform, CI/CDInterviewers also test these - they're common to every stack, whichever one you picked above.
AI Engineer
LLMs, RAG, Agents, EvalsAI-Powered Developer
Claude Code, Copilot, Agentic WorkflowsCore SWE Interview Prep
Data structures, algorithms, OS, concurrency, networking, gitInterviewers also test these - they're common to every stack, whichever one you picked above.
Interviewers also test these - they're common to every stack, whichever one you picked above.