Tech

Software engineering in practice

Write code that a bank can run in production: safe threads, reliable tests, the Java and Python traps that cause real incidents, and the habits reviewers look for.

Read first: Coding and algorithms

7 concepts9 min read25-question test, from a bank of 33Updated

Concept 1 of 71 min

Threads, races, deadlocks and the GIL

The rule
An unguarded counter++ shared by threads loses updates because it reads, adds and writes in 3 steps, so use a lock or an AtomicInteger.
Worked examples

Downloads that wait

Given
Downloads50
Each2 seconds
  1. In sequence = 50 × 2 = 100 seconds
  2. Overlapping: close to 2 seconds

Threads help with waiting, even in Python.

CPU-bound tasks

Given
Tasks4
Each10 seconds
  1. 4 Python threads ≈ 4 × 10 = 40 seconds
  2. 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

Given
Java threads2
Additions each500,000
  1. Expected = 2 × 500,000 = 1,000,000
  2. Usually ends below

Unsynchronised updates get lost, so use a lock or an atomic counter.

The picture
Formula
AtomicInteger.incrementAndGet() or synchronized block
lock(min(id1, id2)) then lock(max(id1, id2))
I/O-bound: threads or asyncio
CPU-bound: multiprocessing
Why it holds

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.

Watch out
  • Calling counter++ a single step.
  • Adding sleeps to cure a deadlock.
  • Using threads to speed up CPU-bound Python.
In the interview
  1. What is a race condition? Give an example and a fix.

  2. 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 rule
Unit tests should be fast and deterministic, so external services are mocked and tested apart, and the clock and time zone are injected.

The why, the charts, the worked example and the interview questions are in Essentials, with every quiz.

See the plansSign in

Concept 3 of 71 min

Java, Python and C++ traps behind real incidents

The rule
Equal Java objects must return equal hash codes or HashMap lookups miss, and an ever-growing static map leaks memory despite garbage collection.

The why, the charts, the worked example and the interview questions are in Essentials, with every quiz.

See the plansSign in

Concept 4 of 71 min

The cost of everyday operations

The rule
list.pop(0) shifts every other item, O(n), while deque.popleft() is O(1).

The why, the charts, the worked example and the interview questions are in Essentials, with every quiz.

See the plansSign in

Concept 5 of 71 min

Shipping safely: secrets, git, versions and flags

The rule
A live API (application programming interface) key pushed to a shared branch must be revoked and rotated before any clean-up.

The why, the charts, the worked example and the interview questions are in Essentials, with every quiz.

See the plansSign in

Concept 6 of 71 min

Design, text encoding, sensitive data and HTTP

The rule
A factory behind a shared interface adds formats without touching callers, and PUT is idempotent, while POST creates again.

The why, the charts, the worked example and the interview questions are in Essentials, with every quiz.

See the plansSign in

Concept 7 of 71 min

AdvancedAmdahl's law: the serial part caps the speed-up

The rule
A job whose share p runs in parallel on N cores takes at best (1 − p) + p ÷ N of its original time.

The why, the charts, the worked example and the interview questions are in Essentials, with every quiz.

See the plansSign in

Recap

  • 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.
Now test it

25 questions on Software engineering in practice

Timed, like the real one, drawn from a bank of 33. Every answer explained.

Test yourself →