LearnThatStack Ace your next interview
Computer Science Fundamentals
Sorting, Searching & Recursion.
Change topic Change
Practice · Questions

All questions

Showing of 75
Beginner 18
01

How does binary search locate a value in a sorted array so quickly?

Beginner ·

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:

Keep going - a few more words and AI can grade it.

Last attempt -

Your answer

Re-explain

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.

Rewriting in plainer words…

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 ↓

Related concept

Tailored explanation · switch back to · ·
What should the new diagram focus on?
How well did you know this?
AI:

02

How does merge sort split a list apart and merge it back sorted?

Beginner ·

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:

Keep going - a few more words and AI can grade it.

Last attempt -

Your answer

Re-explain

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.

Rewriting in plainer words…

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 ↓

Related concept

Tailored explanation · switch back to · ·
What should the new diagram focus on?
How well did you know this?
AI:

03

How does quicksort partition a list around a pivot to sort in place?

Visual

Beginner ·

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:

Keep going - a few more words and AI can grade it.

Last attempt -

Your answer

Re-explain
Visual

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.

Rewriting in plainer words…

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 ↓

Related concept

Tailored explanation · switch back to · ·
What should the new diagram focus on?
How well did you know this?
AI:

04

What are the practical differences between quicksort and merge sort?

Beginner ·

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:

Keep going - a few more words and AI can grade it.

Last attempt -

Your answer

Re-explain

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.

Rewriting in plainer words…

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 ↓

Related concept

Tailored explanation · switch back to · ·
What should the new diagram focus on?
How well did you know this?
AI:

05

What does it mean for a sorting algorithm to be stable?

Beginner ·

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:

Keep going - a few more words and AI can grade it.

Last attempt -

Your answer

Re-explain

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.

Rewriting in plainer words…

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 ↓

Related concept

Tailored explanation · switch back to · ·
What should the new diagram focus on?
How well did you know this?
AI:

06

Which sorting algorithm does your language's standard library actually use?

Beginner ·

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:

Keep going - a few more words and AI can grade it.

Last attempt -

Your answer

Re-explain

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.

Rewriting in plainer words…

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 ↓

Related concept

Tailored explanation · switch back to · ·
What should the new diagram focus on?
How well did you know this?
AI:

07

What is recursion, and what two ingredients does every recursive function need?

Beginner ·

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:

Keep going - a few more words and AI can grade it.

Last attempt -

Your answer

Re-explain

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
Rewriting in plainer words…

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 ↓

Related concept

Tailored explanation · switch back to · ·
What should the new diagram focus on?
How well did you know this?
AI:

08

What is a base case, and what happens when a recursion lacks one?

Beginner ·

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:

Keep going - a few more words and AI can grade it.

Last attempt -

Your answer

Re-explain

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.

Rewriting in plainer words…

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 ↓

Related concept

Tailored explanation · switch back to · ·
What should the new diagram focus on?
How well did you know this?
AI:

09

What is backtracking, and how does it differ from plain brute-force search?

Beginner ·

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:

Keep going - a few more words and AI can grade it.

Last attempt -

Your answer

Re-explain

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.

Rewriting in plainer words…

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 ↓

Related concept

Tailored explanation · switch back to · ·
What should the new diagram focus on?
How well did you know this?
AI:

10

What is dynamic programming, and what kind of problem does it solve?

Beginner ·

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:

Keep going - a few more words and AI can grade it.

Last attempt -

Your answer

Re-explain

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.

Rewriting in plainer words…

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 ↓

Related concept

Tailored explanation · switch back to · ·
What should the new diagram focus on?
How well did you know this?
AI:

11

What is a greedy algorithm, and when does the greedy choice work?

Beginner ·

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:

Keep going - a few more words and AI can grade it.

Last attempt -

Your answer

Re-explain

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.

Rewriting in plainer words…

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 ↓

Related concept

Tailored explanation · switch back to · ·
What should the new diagram focus on?
How well did you know this?
AI:

12

What must be true about your data before you can use binary search?

Part of Pro
13

What are the time and space costs of the common sorting algorithms?

Part of Pro
14

How does the call stack keep track of nested recursive calls?

Part of Pro
15

Why can deep recursion crash a program with a stack overflow?

Part of Pro
16

When is recursion clearer or simpler than an equivalent loop?

Part of Pro
17

What is memoization, and how does caching results speed up recursion?

Part of Pro
18

What are overlapping subproblems, and why do they slow plain recursion?

Part of Pro
Intermediate 27
19

When does quicksort degrade to quadratic time, and what triggers it?

Part of Pro
20

How does the choice of pivot change quicksort's behavior and cost?

Part of Pro
21

Why is quicksort often faster than merge sort in real practice?

Part of Pro
22

How does heapsort work, and why is it rarely the default choice?

Part of Pro
23

When does counting sort beat every comparison-based sorting algorithm?

Part of Pro
24

How does radix sort work, and what kind of data suits it?

Part of Pro
25

Why can no comparison-based sort ever run faster than n log n?

Part of Pro
26

How do you run a binary search over an answer space?

Part of Pro
27

How do you find the first or last match with binary search?

Part of Pro
28

What is the classic overflow bug when computing a binary search midpoint?

Part of Pro
29

Which common sorts are stable, and why does stability cost something?

Part of Pro
30

When and how do you rewrite a recursive function as a loop?

Part of Pro
31

What is tail recursion, and when can it avoid growing the stack?

Part of Pro
32

How do you work out the time complexity of a recursive function?

Part of Pro
33

How does the Master Theorem analyze divide-and-conquer running times?

Part of Pro
34

What general template do most backtracking problems share?

Part of Pro
35

How does pruning make backtracking practical on a large search space?

Part of Pro
36

Why does backtracking so often have exponential worst-case running time?

Part of Pro
37

How does backtracking handle permutations differently from combinations?

Part of Pro
38

How do you stop a backtracking search from producing duplicate solutions?

Part of Pro
39

How do you recognize that a problem is a good fit for dynamic programming?

Part of Pro
40

What is the difference between memoization and tabulation?

Part of Pro
41

How do you define the state and transition for a dynamic programming solution?

Part of Pro
42

When is a greedy algorithm provably correct, and how do you show it?

Part of Pro
43

Why can a greedy choice give a wrong answer, and how do you spot it?

Part of Pro
44

What is optimal substructure, and how does it differ from the greedy-choice property?

Part of Pro
45

How do you cut a DP's memory by keeping only the rows you need?

Part of Pro
Expert 30
46

How would you sort far more data than fits in memory?

Part of Pro
47

How do you sort a huge dataset spread across many machines?

Part of Pro
48

How do cache effects make some n log n sorts faster than others?

Part of Pro
49

How does a hybrid sort like Timsort combine merging and insertion sort?

Part of Pro
50

How does introsort protect quicksort from its quadratic worst case?

Part of Pro
51

How do you make an in-place sort stable, and what does it cost?

Part of Pro
52

How do you sort efficiently when each comparison is very expensive?

Part of Pro
53

How do you parallelize quicksort or merge sort across threads safely?

Part of Pro
54

What breaks in binary search when the array holds billions of elements?

Part of Pro
55

How do you binary search a monotonic function you can only sample?

Part of Pro
56

When do you use quickselect instead of sorting to find the k-th element?

Part of Pro
57

How do you rescue a recursion that overflows the stack on real inputs?

Part of Pro
58

How do you analyze a recursive algorithm's space cost, not just its time?

Part of Pro
59

How does tail-call optimization differ across languages, and which ones lack it?

Part of Pro
60

How do you parallelize a divide-and-conquer algorithm effectively?

Part of Pro
61

How do you bound a backtracking search that could explore billions of states?

Part of Pro
62

How does branch-and-bound improve on plain backtracking search?

Part of Pro
63

How does ordering your choices help backtracking find solutions sooner?

Part of Pro
64

How do you detect and cut symmetric branches in a backtracking search?

Part of Pro
65

When does iterative deepening beat a plain depth-first backtracking search?

Part of Pro
66

How does constraint propagation speed up a backtracking solver?

Part of Pro
67

How do you recover the actual solution, not just its cost, from a DP table?

Part of Pro
68

How do you reconstruct a DP answer while using only linear memory?

Part of Pro
69

How does bitmask DP encode subsets, and where does it stop scaling?

Part of Pro
70

When is a greedy heuristic an acceptable approximation for a hard problem?

Part of Pro
71

How do recursion depth and caching push a large DP toward tabulation?

Part of Pro
72

How do memory-access patterns affect a large DP table's real-world speed?

Part of Pro
73

How do you parallelize a dynamic program given its dependency structure?

Part of Pro
74

What can you do when a DP's state space is too large to enumerate?

Part of Pro
75

How do you choose between greedy, divide-and-conquer, and dynamic programming for a new problem?

Part of Pro

No matches

Try a different filter or search term.

Know someone prepping for Sorting, Searching & Recursion? Send them this set.
Pro · $10/mo

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.

Technologies
No technologies match “”.
Cross-cutting topics
No topics match “”.
By role
Stacks & frameworks

MEAN

MongoDB, Express, Angular, Node.js

MERN

MongoDB, Express, React, Node.js

LAMP

Linux, Apache, MySQL, PHP

Django

Python Full-Stack Development

Ruby on Rails

Convention over Configuration

Serverless on AWS

Serverless Architecture on AWS

Flutter Mobile

Flutter Cross-Platform Mobile Development

Spring Boot

Enterprise Java Development

.NET

Microsoft Ecosystem

Vue

Vue.js, Vite, TypeScript, Tailwind, Node.js

Go Backend

Golang, gRPC, PostgreSQL, Redis, RabbitMQ

FastAPI

Python, FastAPI, SQLAlchemy, PostgreSQL

React Native

React, TypeScript, Redux, Firebase

iOS Native

Swift, SwiftUI, UIKit, Firebase

Android Native

Java, Jetpack Compose, Firebase

DevOps / Platform

Docker, Kubernetes, Terraform, CI/CD

AI Engineer

LLMs, RAG, Agents, Evals

AI-Powered Developer

Claude Code, Copilot, Agentic Workflows

Core SWE Interview Prep

Data structures, algorithms, OS, concurrency, networking, git