LearnThatStack Ace your next interview
Computer Science Fundamentals

Concurrency & Multithreading.

Start free Change topic Change

Practise Concurrency & Multithreading one question at a time. Answer out loud or in writing, get graded, and see exactly what you missed.

Try one question

What is the difference between concurrency and parallelism?

The answer

Concurrency means structuring a program so many tasks make progress in overlapping time windows. Parallelism means literally running tasks at the same instant on separate cores. A single-core machine can be concurrent but never truly parallel.

A concurrent design interleaves tasks by pausing one and resuming another. This helps when tasks wait on input, network, or disk. Parallelism helps when you have real compute to split across cores.

You can have either without the other. A single-threaded event loop is concurrent, not parallel. A numeric loop spread across eight cores is parallel. Most real systems mix both.

Reach for concurrency when tasks mostly wait, and parallelism when tasks mostly compute. Confusing them leads to adding cores that never help, or adding threads to work that has no waiting to overlap.

Example · what grading looks like
What is a race condition and why is it so hard to reproduce?

“A race condition is when two threads access shared data at the same time and the result depends on which one gets there first. For example, if two threads both read a counter, add one and write it back, one of the increments can get lost. It's hard to reproduce because it only happens with a rare timing, and the thread scheduling changes from run to run depending on load and the machine.”

Knew it

They've got the core idea: shared data, timing-dependent outcome, lost increment example, and why rerunning doesn't reproduce it reliably. A real interview answer would add that you fix it with synchronization, not by chasing the one bad run.

Missed: Doesn't mention that the fix is reasoning about shared state and adding synchronization rather than trying to catch the exact crash


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

All 54 questions

Each on its own page

Threads and concurrency basics

How concurrency differs from parallelism, and what threads and context switches cost.

3 questions

  1. What is the difference between concurrency and parallelism?
  2. Why is creating threads expensive, and roughly how expensive?
  3. What does a thread context switch cost under heavy contention? Pro

Race conditions and thread safety

What races and data races are, what makes code thread-safe, and how confinement and actors avoid shared state.

5 questions

  1. What is a race condition and why is it so hard to reproduce?
  2. What does it mean for code to be thread-safe? Pro
  3. What is the difference between a data race and a race condition? Pro
  4. What are thread-local variables, and when would you use them? Pro
  5. What is the actor model, and when would you use it instead of locks? Pro

Mutexes and lock types

Mutexes, critical sections, semaphores, spinning versus blocking, and read-write and reentrant locks.

6 questions

  1. What is a mutex and how does it stop a race condition?
  2. What is a critical section and why keep it as short as possible? Pro
  3. How do a semaphore and a mutex differ in purpose? Pro
  4. What is the difference between blocking and busy-waiting for a lock? Pro
  5. What is a read-write lock and when does it actually pay off? Pro
  6. How does a reentrant lock keep a thread from deadlocking itself? Pro

Condition variables and producer-consumer

Waiting efficiently on conditions, bounded buffers, and backpressure between producers and consumers.

4 questions

  1. What is a condition variable and what problem does it solve? Pro
  2. What is the producer-consumer problem and how is it solved? Pro
  3. How do you make a bounded buffer block when full or empty? Pro
  4. How do you add backpressure to a producer-consumer pipeline that outpaces consumers? Pro

Deadlock, livelock and starvation

Why threads get stuck or never progress, how lock ordering and fairness prevent it, and how to recover live.

6 questions

  1. What is a deadlock and what four conditions must all hold?
  2. How does consistent lock ordering prevent deadlock? Pro
  3. What is livelock and how does it differ from deadlock? Pro
  4. What is starvation, and how does lock fairness reduce it? Pro
  5. How do you detect and recover from a deadlock in a live service? Pro
  6. How do you prevent priority inversion in a latency-sensitive system? Pro

Thread pools and work queues

Why pools reuse threads, how nested tasks can deadlock a pool, and what an unbounded work queue does.

3 questions

  1. What is a thread pool and why reuse threads instead of spawning?
  2. How would you design a thread pool that avoids deadlock on nested tasks? Pro
  3. What goes wrong when a thread pool's work queue grows without bound? Pro

Async I/O and the event loop

How a single-threaded event loop runs async work, when async beats threads, and blocking and cancellation pitfalls.

5 questions

  1. How does the JavaScript event loop run async work on one thread?
  2. When should you reach for async I/O instead of a thread pool? Pro
  3. Why does async/await not speed up CPU-bound work? Pro
  4. What are the failure modes of code that blocks the event loop? Pro
  5. How do you cancel and clean up in-flight async work without leaks or races? Pro

Memory visibility and ordering

Why writes can stay invisible, what volatile and atomic flags guarantee, acquire-release ordering and safe publication.

5 questions

  1. Why can one thread miss another thread's writes without synchronization? Pro
  2. What does a volatile or atomic flag guarantee, and what does it not? Pro
  3. How does double-checked locking break without a memory barrier? Pro
  4. How do acquire and release memory ordering keep a lock-free algorithm correct? Pro
  5. How would you make singleton initialization both safe and nearly lock-free? Pro

Atomics and lock-free algorithms

Atomic operations, compare-and-swap, the ABA problem, lock-free queues, memory reclamation and progress guarantees.

6 questions

  1. What is an atomic operation and when is one enough? Pro
  2. How does compare-and-swap implement a lock-free counter? Pro
  3. How does the ABA problem defeat naive compare-and-swap, and how do you fix it? Pro
  4. How would you build a non-blocking queue that scales across many producers? Pro
  5. How do you safely reclaim memory in a lock-free data structure? Pro
  6. What is the difference between lock-free and wait-free algorithms? Pro

Lock contention and scaling

How contention, lock granularity, striping and false sharing limit throughput as threads and cores increase.

7 questions

  1. How does lock contention limit throughput as thread count rises? Pro
  2. How does lock granularity trade contention against overhead? Pro
  3. How would you make a shared counter class thread-safe with minimal contention? Pro
  4. What is false sharing and how can it silently destroy parallel throughput? Pro
  5. What breaks in a lock-based cache at a million requests per second? Pro
  6. Why can adding more threads slow a program down, and where is the knee? Pro
  7. How would you shard or stripe a lock to cut contention safely? Pro

Debugging and testing concurrent code

Why races slip past tests, diagnosing ones that appear only under load, reviewing code for concurrency bugs, and stress-testing to expose rare interleavings.

4 questions

  1. Why do race conditions hide in testing but surface in production? Pro
  2. How would you diagnose a race that appears only under production load? Pro
  3. How would you review unfamiliar code for concurrency bugs you cannot reproduce? Pro
  4. How would you stress-test concurrent code to surface rare interleavings? Pro
Practice · Questions

All questions

Showing of 54
Beginner 13
01

What is the difference between concurrency and parallelism?

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

Concurrency means structuring a program so many tasks make progress in overlapping time windows. Parallelism means literally running tasks at the same instant on separate cores. A single-core machine can be concurrent but never truly parallel.

A concurrent design interleaves tasks by pausing one and resuming another. This helps when tasks wait on input, network, or disk. Parallelism helps when you have real compute to split across cores.

You can have either without the other. A single-threaded event loop is concurrent, not parallel. A numeric loop spread across eight cores is parallel. Most real systems mix both.

Reach for concurrency when tasks mostly wait, and parallelism when tasks mostly compute. Confusing them leads to adding cores that never help, or adding threads to work that has no waiting to overlap.

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

What is a race condition and why is it so hard to reproduce?

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

A race condition is a bug where the outcome depends on the timing of two or more threads. When they touch shared data and the order is not controlled, results vary from run to run.

Take two threads each reading a counter, adding one, and writing back. If both read the same value before either writes, one increment is lost. The count is wrong only for certain interleavings.

It is hard to reproduce because the bad interleaving is rare. Scheduling is nondeterministic and shifts with load, timing, and the machine. That is why rerunning does not debug a race. You fix it by reasoning about shared state and adding synchronization, not by hunting for the one crash.

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

Why is creating threads expensive, and roughly how expensive?

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

Creating a thread is expensive because each one needs its own stack and kernel bookkeeping. The operating system allocates memory, registers the thread with its scheduler, and sets up later cleanup.

The biggest cost is the stack. A default thread stack reserves from half a megabyte to eight, depending on the platform. Spawn thousands and you burn gigabytes of address space on stacks alone.

In wall-clock terms, creating and destroying a thread takes tens of microseconds. That dwarfs the microsecond-scale work you often hand it, so per-request spawning spends more on setup than on the task.

This is why servers reuse threads from a pool instead of making one per request. The cost is in the constant creation and teardown under load.

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 is a mutex and how does it stop a race condition?

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 mutex, short for mutual exclusion lock, is a gate that only one thread can hold at a time. A thread locks it before touching shared data and unlocks it when done. Others must wait.

By forcing threads to take turns, a mutex serializes access to a critical section. That is how it stops a race condition. No two threads read, modify, and write the same value at once, so the lost-update problem vanishes.

mutex.lock();
counter = counter + 1; // no other thread runs this line now
mutex.unlock();

A mutex trades some throughput for correctness. While one thread holds it, the others block and do nothing. Lock only the shared state, and always release the mutex, even when errors happen.

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 is a deadlock and what four conditions must all hold?

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

A deadlock is a standstill where two or more threads each wait for a resource another holds. None can proceed, so all stay stuck forever. The program hangs without crashing.

All four Coffman conditions must hold at once:

  • Mutual exclusion: only one thread can hold a resource.
  • Hold and wait: a thread keeps what it has while waiting for more.
  • No preemption: you cannot forcibly take a resource away.
  • Circular wait: a cycle of threads each waits on the next.

Break any one and deadlock cannot form. The most common fix targets circular wait by making every thread acquire locks in the same order.

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

What is a thread pool and why reuse threads instead of spawning?

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 thread pool is a fixed set of worker threads that pull tasks from a shared queue. You submit work, and an idle worker picks it up, runs it, then loops back for more. The workers live for the life of the program.

Reusing threads skips the cost of creating and destroying one per task. That setup, tens of microseconds each, adds up fast under load.

A pool also caps concurrency. You never launch ten thousand threads and exhaust memory or thrash the scheduler, because the queue absorbs bursts instead.

The tradeoff is sizing. Too few workers leave tasks waiting, and too many fight over cores. Use roughly the core count for compute, and more for I/O-bound work.

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

How does the JavaScript event loop run async work on one thread?

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

The JavaScript event loop lets the system do the waiting for async work, then runs each callback on its one thread. It takes the next callback from a queue, runs it to completion, then grabs the next.

When you start a timer, network request, or file read, the runtime hands it to the system or a background pool. Your thread moves on right away.

When the operation finishes, its callback joins a queue. The loop runs it only after the current work returns and the stack is empty. So a ready callback still waits behind the running code.

This model avoids locks, since only one callback runs at a time. The catch is that any slow synchronous function freezes everything. One blocking loop stalls every pending timer, request, and click.

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 critical section and why keep it as short as possible?

Part of Pro
09

How do a semaphore and a mutex differ in purpose?

Part of Pro
10

What is a condition variable and what problem does it solve?

Part of Pro Visual
11

What is an atomic operation and when is one enough?

Part of Pro
12

What is the producer-consumer problem and how is it solved?

Part of Pro
13

What does it mean for code to be thread-safe?

Part of Pro
Intermediate 20
14

Why do race conditions hide in testing but surface in production?

Part of Pro
15

When should you reach for async I/O instead of a thread pool?

Part of Pro
16

What is the difference between a data race and a race condition?

Part of Pro
17

How does lock contention limit throughput as thread count rises?

Part of Pro Visual
18

What is the difference between blocking and busy-waiting for a lock?

Part of Pro
19

How does consistent lock ordering prevent deadlock?

Part of Pro Visual
20

What is livelock and how does it differ from deadlock?

Part of Pro Visual
21

What is starvation, and how does lock fairness reduce it?

Part of Pro Visual
22

What is a read-write lock and when does it actually pay off?

Part of Pro
23

How does compare-and-swap implement a lock-free counter?

Part of Pro Visual
24

Why can one thread miss another thread's writes without synchronization?

Part of Pro Visual
25

What does a volatile or atomic flag guarantee, and what does it not?

Part of Pro Visual
26

How does double-checked locking break without a memory barrier?

Part of Pro Visual
27

How does lock granularity trade contention against overhead?

Part of Pro
28

How do you make a bounded buffer block when full or empty?

Part of Pro Visual
29

What does a thread context switch cost under heavy contention?

Part of Pro
30

Why does async/await not speed up CPU-bound work?

Part of Pro
31

How does a reentrant lock keep a thread from deadlocking itself?

Part of Pro
32

What are thread-local variables, and when would you use them?

Part of Pro Visual
33

What is the actor model, and when would you use it instead of locks?

Part of Pro Visual
Expert 21
34

How would you make a shared counter class thread-safe with minimal contention?

Part of Pro
35

What is false sharing and how can it silently destroy parallel throughput?

Part of Pro Visual
36

How does the ABA problem defeat naive compare-and-swap, and how do you fix it?

Part of Pro Visual
37

How would you design a thread pool that avoids deadlock on nested tasks?

Part of Pro Visual
38

How do you detect and recover from a deadlock in a live service?

Part of Pro
39

How would you build a non-blocking queue that scales across many producers?

Part of Pro
40

What breaks in a lock-based cache at a million requests per second?

Part of Pro Visual
41

How do acquire and release memory ordering keep a lock-free algorithm correct?

Part of Pro Visual
42

How would you diagnose a race that appears only under production load?

Part of Pro
43

How do you add backpressure to a producer-consumer pipeline that outpaces consumers?

Part of Pro Visual
44

What goes wrong when a thread pool's work queue grows without bound?

Part of Pro
45

How would you make singleton initialization both safe and nearly lock-free?

Part of Pro
46

How do you prevent priority inversion in a latency-sensitive system?

Part of Pro Visual
47

Why can adding more threads slow a program down, and where is the knee?

Part of Pro
48

How would you review unfamiliar code for concurrency bugs you cannot reproduce?

Part of Pro
49

How would you shard or stripe a lock to cut contention safely?

Part of Pro Visual
50

How do you safely reclaim memory in a lock-free data structure?

Part of Pro Visual
51

What are the failure modes of code that blocks the event loop?

Part of Pro Visual
52

How do you cancel and clean up in-flight async work without leaks or races?

Part of Pro
53

How would you stress-test concurrent code to surface rare interleavings?

Part of Pro
54

What is the difference between lock-free and wait-free algorithms?

Part of Pro

No matches

Try a different filter or search term.

Know someone prepping for Concurrency & Multithreading? Send them this set.
Quick recap

Concurrency & Multithreading in pictures.

A quick recap of Concurrency & Multithreading in 31 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 27 pictures come with Pro.

Get Pro · $10/mo
Pro · $10/mo

47 of 54 Concurrency & Multithreading 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