Concurrency and multithreading: threads, locks, races and async I/O – Server Side interview prep
261 interview-style multiple-choice questions on Concurrency and multithreading: threads, locks, races and async I/O, organized by topic: concepts, scenarios, code to read and tradeoffs, each with a short explanation. Learn the 69 key terms with the flashcards first, then test yourself.
Take the practice test · Flashcards · All study tools
What it covers
- 12.1 Processes, threads and scheduling
- 12.2 Race conditions, visibility and memory models
- 12.3 Locks and synchronization primitives
- 12.4 Atomics, lock-free code and contention
- 12.5 Deadlock, livelock and starvation
- 12.6 Thread pools, tasks, futures and structured concurrency
- 12.7 Async I/O, event loops and coroutines
- 12.8 Message passing: queues, channels and actors
- 12.9 Concurrency patterns, distributed coordination and testing
Key terms
12.1 Processes, threads and scheduling
- Concurrency
- Making progress on several tasks by interleaving
- Parallelism
- Running tasks simultaneously on multiple cores
- Thread pool
- Reusable worker threads for queued tasks
- Amdahl's law
- Serial parts limit parallel speedup
- Preemption
- The OS interrupting a running thread
- Green thread
- Runtime-scheduled lightweight thread
- Gustafson's law
- Bigger problems scale with more cores
- Little's law
- L = λW
- Copy-on-write
- Share pages until written
- NUMA
- Memory speed depends on socket
12.2 Race conditions, visibility and memory models
- Race condition
- Timing-dependent bug on shared state
- Mutex
- Lock allowing one thread in a critical section
- Atomic operation
- Indivisible operation on shared memory
- Semaphore
- Counter limiting concurrent access
- Data race
- Unsynchronized access with at least one write
- Check-then-act
- Acting on a check that may be stale
- Happens-before
- Ordering that guarantees visibility
- Memory barrier
- Stops reordering across a point
- Safe publication
- Sharing an object fully built
- Thread confinement
- Data used by only one thread
12.3 Locks and synchronization primitives
- Critical section
- Code that must not run concurrently
- Reentrant lock
- Owner can reacquire it
- Spinlock
- Busy-waits for a lock
- Condition variable
- Wait until state changes
- Barrier
- All threads wait at a point
- Lock striping
- Separate locks per segment
12.4 Atomics, lock-free code and contention
- Compare-and-swap
- Conditional atomic update
- ABA problem
- Value changes and returns, fooling CAS
- Lock-free
- Some thread always progresses
- Wait-free
- Every thread progresses in bounded steps
- False sharing
- Separate variables on one cache line
- RCU
- Lock-free reads, versioned updates
12.5 Deadlock, livelock and starvation
- Deadlock
- Threads waiting on each other forever
- Livelock
- Threads busy reacting but making no progress
- Starvation
- A thread never getting needed resources
- Lock ordering
- Acquiring locks in a fixed global order
- Dining philosophers
- Classic deadlock and starvation example
- Wait-for graph
- Who waits on whom
- Banker's algorithm
- Deadlock avoidance by safe states
- Priority inheritance
- Lock holder borrows waiter's priority
- Aging
- Raising priority while waiting
- Thread dump
- Snapshot of all threads and locks
12.6 Thread pools, tasks, futures and structured concurrency
- Future
- Placeholder for a later result
- Work stealing
- Idle workers take others' queued tasks
- Rejection policy
- What a full pool does with new tasks
- Structured concurrency
- Child tasks bound to a scope
- Cooperative cancellation
- Tasks check for cancel requests
- Thread pool starvation
- All threads blocked, work stuck
12.7 Async I/O, event loops and coroutines
- Non-blocking I/O
- Calls return immediately if not ready
- epoll
- Linux readiness notification
- io_uring
- Linux completion-based I/O rings
- Reactor pattern
- React to readiness, then do I/O
- Coroutine
- Function that suspends and resumes
- Function coloring
- Async spreading through call chains
12.8 Message passing: queues, channels and actors
- Producer-consumer
- Queue between work producers and consumers
- Actor model
- Message-passing actors with private state
- Optimistic concurrency
- Check for conflicts at commit time
- Fan-out/fan-in
- Spread work out, then gather results
- Blocking queue
- Queue whose put/take wait
- SPSC queue
- Single producer, single consumer
- CSP
- Processes communicating over channels
- Supervision tree
- Supervisors restart failed children
- Virtual actor
- Actor activated on demand
- Poison pill
- Message telling a consumer to stop
12.9 Concurrency patterns, distributed coordination and testing
- Fencing token
- Monotonic number rejecting stale lock holders
- Lease
- Time-limited, renewable lock
- Deterministic simulation
- Replayable testing with simulated I/O and time
- TLA+
- Specification language for concurrent designs
- Single-writer principle
- One owner for all writes
Sample practice questions
What is the difference between concurrency and parallelism?
- They mean exactly the same thing and are used interchangeably by engineers
- Concurrency only applies to databases; parallelism only applies to CPUs
- Parallelism interleaves many tasks; concurrency runs them at once on several cores
- Concurrency interleaves many tasks; parallelism runs them at once on several cores
Answer: D. Concurrency interleaves many tasks; parallelism runs them at once on several cores A single-core machine can be concurrent but not parallel.
What is a race condition?
- A loop whose exit condition is never reached
- A benchmark competition between two programs for speed
- A bug where the result depends on unpredictable thread timing
- One thread consistently running faster than another
Answer: C. A bug where the result depends on unpredictable thread timing Races often only show up under load.
What does a mutex (lock) guarantee?
- Threads run strictly in the order they were created
- Only one thread at a time runs the critical section
- The protected code always runs faster than before
- No thread ever has to wait to enter the section
Answer: B. Only one thread at a time runs the critical section Hold locks for as short a time as possible.
What does an atomic operation provide?
- Encryption of the value while it's in memory
- An indivisible read-modify-write, like compare-and-swap
- A guarantee that the code only runs on one CPU core
- Automatic retries whenever the operation fails
Answer: B. An indivisible read-modify-write, like compare-and-swap Atomics are cheaper than locks for simple counters and flags.
What are the four Coffman conditions that must all hold for a deadlock?
- Shared memory, busy waiting, priority boost, linear wait
- Mutual exclusion, hold and wait, no preemption, circular wait
- Mutual exclusion, starvation, preemption, lock ordering
- Read, write, lock and unlock all happening concurrently
Answer: B. Mutual exclusion, hold and wait, no preemption, circular wait Breaking any one of them prevents deadlock.
What is a thread pool?
- Reusable worker threads that run queued tasks
- A pool of open connections to a database
- A group of processes spread across machines
- A list of finished threads waiting to be deleted
Answer: A. Reusable worker threads that run queued tasks Pools also cap concurrency so the system isn't overwhelmed.
Which kind of work benefits most from async, non-blocking I/O?
- Work that never has to wait on anything
- I/O-bound work, like many network or database calls
- CPU-bound work, like compressing large images
- A single short calculation done in memory
Answer: B. I/O-bound work, like many network or database calls For CPU-bound work, use parallel threads or processes instead.
What is the main benefit of an immutable object in concurrent code?
- Threads can share it safely without locks
- It runs faster when only one thread uses it
- It always uses less memory than a mutable one
- It can never be garbage collected by the runtime
Answer: A. Threads can share it safely without locks Prefer immutability and message passing over shared mutable state.
What is a common way to make a shared counter in a web app correct across several server instances?
- An atomic op in a shared store, like Redis `INCR`
- A static variable in the server's application code
- A local mutex inside each server instance's process
- Incrementing the counter in each user's browser
Answer: A. An atomic op in a shared store, like Redis `INCR` In-process locks don't coordinate across machines.
What is the difference between a process and a thread?
- They're identical in all modern operating systems today
- A thread can contain many processes, each with its own memory
- Processes have separate memory; a process's threads share it
- Threads have separate memory; processes share one memory space
Answer: C. Processes have separate memory; a process's threads share it Shared memory makes threads cheap to communicate but prone to races.
More Server Side
- HTTP fundamentals
- REST API design
- GraphQL
- gRPC & real-time APIs
- OAuth 2.0, OIDC & JWT
- Sessions, cookies & web app security
- C# & .NET
- Java & Spring Boot
- Node.js & Express
- Python, Django & FastAPI
- Go (Golang)
- OOP & SOLID
- Design patterns
- Dependency injection
- Logging & observability
- Data structures
- Algorithms & Big-O
- Linux & shell scripting