Concurrency Coding Problems

The classic multithreaded interview problems (print in order, alternating output, FizzBuzz with four threads, H2O, dining philosophers, a traffic light, a crawler), each solved with the primitive that fits and tested with real threads.

Overview

Concurrency coding problems give you a class whose methods are called from several threads at once, and ask you to add synchronization so the output is always correct, whatever order the threads run in. They are short, but they test whether you can name the constraint and pick the primitive that enforces it, rather than sprinkling locks around.

Most of them are one of three shapes: an order between threads (A before B, or A and B taking turns), a group that must form before anyone proceeds, or shared resources that must not deadlock. LeetCode's Concurrency list has nine problems: Print in Order, Print FooBar Alternately, Print Zero Even Odd, Building H2O, Design Bounded Blocking Queue, Fizz Buzz Multithreaded, The Dining Philosophers, Web Crawler Multithreaded and Traffic Light Controlled Intersection.

Picking the primitive

Ask three questions in order: what state is shared, what ordering is required, and which primitive states that ordering most directly. Start from what must be guaranteed, not from a favorite lock, and let the answer lead you to the simplest tool that states it.

  • If one step must happen after another just once, use an event or a latch, as in Print in Order. If two threads take turns, use two semaphores, as in FooBar.
  • If a thread may proceed only when shared state says so, use a lock and a condition with a while loop, as in FizzBuzz and the bounded queue.
  • If a group must form before anyone goes on, use semaphores and a barrier, as in H2O.
  • If a thread needs several locks at once, impose a global lock order, as in Dining Philosophers.

Each primitive states one sentence about the program, and each sentence has a classic problem built around it.

  • Event (a one-way flag): 'this has happened'. Print in Order uses it.
  • Semaphore: 'N may pass', or a turn handed from one thread to another. FooBar, H2O and bounded pools use it.
  • Lock + condition variable: 'wait until this shared state is true'. FizzBuzz, Zero Even Odd and the bounded queue use it.
  • Barrier: 'all of us have arrived'. H2O uses it.
  • Lock ordering: 'locks are always taken in the same order'. Dining Philosophers uses it.
  • Lock + set: 'check and add is one step'. Web Crawler uses it.

Every solution below starts its threads in a scrambled order and asserts on the whole output, which is how you should test these in an interview too.

Interview questions

  • What are the most common concurrency coding problems, and what does each test?
  • How do you avoid deadlock in the Dining Philosophers problem? Compare the options.
  • How do you solve the producer-consumer problem?
  • Design a multithreaded web crawler that visits each page on one host exactly once.
  • Implement the traffic light for a two-road intersection.
  • How do you test concurrent code so a pass means something?

Sign up free to read the full topic

More Low-Level Design topics

Start free on Cariara · Coding problems · System design studio