Concept 1 of 71 min
Threads, races, deadlocks and the GIL
Downloads that wait
| Downloads | 50 |
|---|---|
| Each | 2 seconds |
- In sequence = 50 × 2 = 100 seconds
- Overlapping: close to 2 seconds
Threads help with waiting, even in Python.
CPU-bound tasks
| Tasks | 4 |
|---|---|
| Each | 10 seconds |
- 4 Python threads ≈ 4 × 10 = 40 seconds
- 4 processes: about 10 seconds
The GIL (global interpreter lock) runs 1 thread at a time, so CPU (central processing unit)-bound Python work needs processes.
A race on a shared int
| Java threads | 2 |
|---|---|
| Additions each | 500,000 |
- Expected = 2 × 500,000 = 1,000,000
- Usually ends below
Unsynchronised updates get lost, so use a lock or an atomic counter.
2 threads that take 2 locks in opposite orders can deadlock, so lock in one global order.
In CPython the GIL lets threads or asyncio overlap waiting on I/O but not computing, which needs processes. 2 threads can read the same value, both add 1 and both write it back: one update vanishes.
A deadlock needs a cycle of threads each holding what the next one wants; a fixed order, such as by account ID, makes the cycle impossible, while sleeps only make it rarer.
An I/O-bound job leaves the CPU idle while it waits, and the GIL is released during I/O, so threads or asyncio overlap the waits.
A CPU-bound job runs one thread of Python bytecode at a time, so multiprocessing, or native code that releases the GIL, is what uses all cores.
- Calling counter++ a single step.
- Adding sleeps to cure a deadlock.
- Using threads to speed up CPU-bound Python.
What is a race condition? Give an example and a fix.
2 transfers between the same accounts sometimes freeze your payment service. How do you find and fix the cause?
A complete answer covers
- Take a thread dump: look for threads each waiting on a lock the other holds.
- That is a deadlock from locks taken in opposite orders.
- Lock accounts in one global order, for example by ID.
- Add a timeout on lock acquisition and an alert.
Also in our technical interviews, on camera, with a score.
Concept 2 of 71 min
Tests that are fast, deterministic and useful
The why, the charts, the worked example and the interview questions are in Essentials, with every quiz.
See the plansSign inConcept 3 of 71 min
Java, Python and C++ traps behind real incidents
The why, the charts, the worked example and the interview questions are in Essentials, with every quiz.
See the plansSign inConcept 4 of 71 min
The cost of everyday operations
The why, the charts, the worked example and the interview questions are in Essentials, with every quiz.
See the plansSign inConcept 5 of 71 min
Shipping safely: secrets, git, versions and flags
The why, the charts, the worked example and the interview questions are in Essentials, with every quiz.
See the plansSign inConcept 6 of 71 min
Design, text encoding, sensitive data and HTTP
The why, the charts, the worked example and the interview questions are in Essentials, with every quiz.
See the plansSign inConcept 7 of 71 min
AdvancedAmdahl's law: the serial part caps the speed-up
The why, the charts, the worked example and the interview questions are in Essentials, with every quiz.
See the plansSign inRecap
- counter++ is read, add, write: shared state needs a lock or an atomic; lock in one global order to avoid deadlocks.
- Python threads overlap waiting, not computing: asyncio or threads for I/O, processes for CPU because of the GIL.
- Unit tests are fast and deterministic: mock external services, inject the clock, pin behaviour before refactoring.
- Equal Java objects need equal hash codes; money goes in integer ticks or Decimal; a 32-bit int stops near 2.1 billion.
- Rotate a leaked key first, never rebase shared history, ship risky features behind flags and keep pull requests small.
25 questions on Software engineering in practice
Timed, like the real one, drawn from a bank of 33. Every answer explained.