Interview questions
What's the difference between a process and a thread? When would you use each?
How I'd answer: "A process is a running program with its own private memory, given to it by the OS through virtual memory. A thread is a unit of execution inside a process — threads of the same process share the heap and globals but each has its own stack and registers.
Threads are cheaper to create and switch between, and they communicate just by sharing memory — but that sharing means I have to guard shared data with locks or atomics, and one thread crashing takes down the whole process. Processes are isolated: a crash or memory leak in one doesn't affect the others, but they need IPC like pipes or sockets to talk.
So I'd use threads for tasks that share a lot of data, like a thread pool processing items from one in-memory queue. I'd use processes for isolation, or in Python to get real CPU parallelism around the GIL — which is why servers like Gunicorn run multiple worker processes."
Combines: Ch 3, 5, 7, 8.
Two threads increment a shared counter a million times each. The result is less than two million. Why, and how do you fix it?
How I'd answer:
"count++ isn't atomic — it's a load, an add and a store. If thread A loads 10, then thread B also loads
10 before A stores, both store 11 and one increment is lost. The scheduler can interleave them anywhere,
so the result is random and always too low.
Three fixes: put the increment in a critical section with a lock — synchronized in Java; use an atomic
type like AtomicInteger, which uses a CPU compare-and-swap instruction; or avoid sharing entirely by
giving each thread its own counter and summing at the end, which is usually the fastest. The same bug
appears across web requests reading and writing a database, and there the fix is an atomic update or a
row lock."
Our service's pods keep restarting in Kubernetes with exit code 137. What's going on?
How I'd answer:
"137 is 128 plus 9 — the process was killed with SIGKILL. The most common reason is the OOM killer: the
container went over its memory limit. Kubernetes usually shows it as OOMKilled in the pod status.
I'd check the memory graph: if memory climbs steadily until each restart, it's likely a leak — for example a cache or a global list that only grows, so the garbage collector can't free it. If memory spikes, it's a big allocation, like loading a whole file or query result into memory at once. The fixes are finding the leak, streaming instead of loading everything, or — only if the usage is legitimate — raising the memory limit. The other cause of 137 is a failed graceful shutdown: if the app ignores SIGTERM, it gets SIGKILLed after the grace period."
Combines: Ch 5 (OOM killer), Ch 6 (leaks), Ch 12 (signals, exit codes).
What's a deadlock, and how would you prevent one in a money-transfer function?
How I'd answer: "A deadlock is when threads each hold a lock and wait for another's, in a cycle, so none can move. It needs four conditions: mutual exclusion, hold-and-wait, no preemption and circular wait.
In a transfer from account A to B, if one thread locks A then B while another transfers B to A and locks B then A, they can deadlock. The standard prevention is a global lock order — always lock the account with the smaller ID first, regardless of the transfer direction. That removes the circular wait. I'd also keep a timeout as a safety net. In a database, the engine detects deadlocks and aborts one transaction, so the code should retry on a deadlock error."
Combines: Ch 8, 9.
Why can Node.js or FastAPI handle thousands of concurrent connections on one thread, and what's the catch?
How I'd answer:
"Most web requests are I/O-bound — they spend their time waiting for a database or another API. With
blocking I/O you'd need a thread per waiting request. An event loop instead uses non-blocking I/O and the
OS's multiplexing — epoll on Linux, kqueue on Mac — so one thread asks the kernel 'tell me which of
these thousand sockets are ready', handles those, and goes back to waiting. Waiting costs almost nothing,
so one thread can juggle thousands of connections.
The catch is that everything shares that one thread. If a handler does heavy CPU work or makes a
blocking call — like a synchronous database driver or time.sleep — every other request waits. So CPU
work has to go to a thread pool, a separate process, or a background worker."