Practise Sorting, Searching & Recursion one question at a time. Answer out loud or in writing, get graded, and see exactly what you missed.
How does binary search locate a value in a sorted array so quickly?
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 where the speed comes from. 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.
Missed:
“Recursion is when a function solves a problem by calling itself on a smaller piece of the same problem, like computing a factorial as n times the factorial of n minus one. The two things every recursive function needs are a base case, a small input you can answer directly so the calls stop, and a recursive step where the function calls itself again. If you leave out the base case, the function just keeps calling itself until the stack overflows.”
Knew itThey have both ingredients right and the factorial example is solid. In a real interview they'd want to add that the recursive step has to shrink toward the base case, not just call itself again.
Missed: Didn't say the recursive step must shrink the problem and move toward the base case, just that it 'calls itself again'.
Get your own answers graded
Sign up free. Your account comes with 50 credits for grading, and it remembers what you missed.
What it covers
- Binary search
- Recursion and the call stack
- Merge sort, quicksort and heapsort
- Backtracking fundamentals
- Stability and non-comparison sorts
- Greedy algorithms
- Pivots, worst cases and quickselect
- Analyzing recursive algorithms
- Stack depth and tail calls
- Choosing a sort in practice
- Pruning and smarter backtracking
- Sorting at scale and in parallel
All 61 questions
Binary search
How binary search halves a sorted range, when it applies, boundary and answer-space variants, and the midpoint overflow bug.
- How does binary search locate a value in a sorted array so quickly?
- What must be true about your data before you can use binary search? Pro
- When would you choose linear search over binary search? Pro
- How do you run a binary search over an answer space? Pro
- How do you find the first or last match with binary search? Pro
- What is the classic overflow bug when computing a binary search midpoint? Pro
- What breaks in binary search when the array holds billions of elements? Pro
- How do you binary search a monotonic function you can only sample? Pro
Recursion and the call stack
What recursion is, base cases, how the call stack tracks nested calls, and when recursion reads better than a loop.
Merge sort, quicksort and heapsort
How the main comparison sorts work, their time and space costs, and why quicksort usually wins in practice.
- How does merge sort split a list apart and merge it back sorted?
- How does quicksort partition a list around a pivot to sort in place?
- What are the practical differences between quicksort and merge sort?
- What are the time and space costs of the common sorting algorithms? Pro
- Why is quicksort often faster than merge sort in real practice? Pro
- How does heapsort work, and why is it rarely the default choice? Pro
Backtracking fundamentals
The choose-explore-unchoose template, why it is exponential, and generating permutations and combinations without duplicates.
- What is backtracking, and how does it differ from plain brute-force search?
- What general template do most backtracking problems share? Pro
- Why does backtracking so often have exponential worst-case running time? Pro
- How does backtracking handle permutations differently from combinations? Pro
- How do you stop a backtracking search from producing duplicate solutions? Pro
Stability and non-comparison sorts
What a stable sort is and what stability costs, the n log n comparison lower bound, and how counting and radix sort get past it.
- What does it mean for a sorting algorithm to be stable?
- When does counting sort beat every comparison-based sorting algorithm? Pro
- How does radix sort work, and what kind of data suits it? Pro
- Why can no comparison-based sort ever run faster than n log n? Pro
- Which common sorts are stable, and why does stability cost something? Pro
- How do you make an in-place sort stable, and what does it cost? Pro
Greedy algorithms
When the greedy choice works, how to prove it correct, how to spot when it fails, and when a greedy approximation is good enough.
Pivots, worst cases and quickselect
How pivot choice drives quicksort's cost, what triggers quadratic time, how introsort guards it, and quickselect for the k-th element.
Analyzing recursive algorithms
Writing and solving recurrences, applying the Master Theorem, and measuring a recursion's stack space as well as its time.
Stack depth and tail calls
Why deep recursion overflows the stack, tail recursion and which languages optimize it, and rewriting recursion as a loop.
- Why can deep recursion crash a program with a stack overflow? Pro
- When and how do you rewrite a recursive function as a loop? Pro
- What is tail recursion, and when can it avoid growing the stack? Pro
- How do you rescue a recursion that overflows the stack on real inputs? Pro
- How does tail-call optimization differ across languages, and which ones lack it? Pro
Choosing a sort in practice
What standard libraries actually use, how hybrids like Timsort work, cache effects, expensive comparisons, and picking the right sort.
- Which sorting algorithm does your language's standard library actually use?
- How do you choose the best sorting algorithm for a problem? Pro
- How do cache effects make some n log n sorts faster than others? Pro
- How does a hybrid sort like Timsort combine merging and insertion sort? Pro
- How do you sort efficiently when each comparison is very expensive? Pro
Pruning and smarter backtracking
Making large backtracking searches practical with pruning, branch-and-bound, choice ordering, symmetry breaking, constraint propagation and iterative deepening.
- How does pruning make backtracking practical on a large search space? Pro
- How do you bound a backtracking search that could explore billions of states? Pro
- How does branch-and-bound improve on plain backtracking search? Pro
- How does ordering your choices help backtracking find solutions sooner? Pro
- How do you detect and cut symmetric branches in a backtracking search? Pro
- When does iterative deepening beat a plain depth-first backtracking search? Pro
- How does constraint propagation speed up a backtracking solver? Pro
Sorting at scale and in parallel
External merge sort for data beyond memory, distributed sorting across machines, and parallelizing divide-and-conquer across threads.
All questions
Showing of 61How 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 where the speed comes from. 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 smaller one, 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 use 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.
Their worst cases differ. 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 their 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 or guaranteed timing.
- Pick merge sort for linked lists and external files too.
This split is why array libraries often tune quicksort, while stable and external sorts use 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 two-level ordering with no extra comparison logic.
An unstable sort can swap equal keys unpredictably. You lose that earlier arrangement, and multi-pass sorting stops working. Merge sort and insertion sort are naturally stable, while plain quicksort and heapsort are not.
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 standard libraries use a hybrid sort tuned for real data, not a single textbook algorithm. Java's object sort uses Timsort, which blends merge sort with insertion sort and exploits runs that are already ordered. Python used Timsort until version 3.11 and now uses Powersort, a Timsort variant with a better rule for which runs to merge next.
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 follow. Sorts of objects tend to be stable, often Timsort or a variant of it. 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 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:
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, because the problem never shrinks to something answerable. Each call reserves a frame on the call stack for its local state. The frames pile up until the stack runs out of room, and the program crashes with a stack overflow.
A wrong base case fails the same way. If your condition never matches the values you reach, the recursion skips the exit and never stops. 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 work. It explores a tree of partial choices, going deeper while things look valid and retreating when they do not.
The difference from brute force is when you check. Brute force 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. Backtracking tests constraints during construction and prunes dead branches before finishing them.
That early pruning is where the savings come from. On problems like placing queens or filling a grid, backtracking skips huge regions of hopeless combinations, while brute force builds and checks them all.
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 taking the option that looks best. It never revisits a choice, which keeps it fast and simple, usually O(n log n) after a sort, sometimes O(n).
Greedy works when two things hold. The first is the greedy-choice property, where a locally best pick is part of some overall best answer. The second is optimal substructure, where solving what remains after that pick gives the full solution.
Making change in US coins works greedily. You take the biggest coin that fits, 25 cents first, then 10, 5 and 1. It fails on unusual coin sets, where a smaller first coin wins.
An unproven greedy pick can return a wrong answer with no error, so test it against a slower exact method.
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?
When would you choose linear search over binary search?
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?
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?
How do you choose the best sorting algorithm for a problem?
When do you use quickselect instead of sorting to find the k-th element?
How do you analyze a recursive algorithm's space cost, not just its time?
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?
How do you rescue a recursion that overflows the stack on real inputs?
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?
When is a greedy heuristic an acceptable approximation for a hard 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 in pictures.
A quick recap of Sorting, Searching & Recursion in 32 short animated pictures, one idea at a time. About 8 min to get the whole topic back in your head before you walk in. Pause any picture, slow it down, or step through it.
Pro The first chapter is free to play. The other 28 pictures come with Pro.
Sorting, Searching & Recursion cheatsheet
- Binary search01
- How the main sorts work02
- Stable & library sorts03
- Faster than n log n04
- Sorting at scale05
- Recursion & the call stack06
- Cost of a recursion07
- Backtracking08
- Pruning big searches09
- Greedy algorithms10
- The model11
- + 5 more inside
This picture is part of Pro. The first chapter is free to play; the other 28 pictures come with Pro.
Get Pro · $10/mo Already Pro? Sign in51 of 61 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.