The Print in Order concurrency problem asks you to ensure that three methods — first(), second(), third() — are called in order across multiple threads, regardless of the order threads are launched. This easy concurrency coding problem uses synchronization primitives: semaphores, locks, or condition variables. The concurrency interview pattern is demonstrated.
Microsoft, Nvidia, and Google ask this as an introduction to concurrent programming. It tests knowledge of basic synchronization: how to make one thread wait for another to complete. This pattern appears in producer-consumer scenarios, pipeline stages, and sequential task execution.
Semaphores (or mutex + condition variables). Initialize two semaphores: sem2 = 0 (second waits for first), sem3 = 0 (third waits for second). first(): run, then signal sem2. second(): wait sem2, run, signal sem3. third(): wait sem3, run.
Threads running in order 3,1,2:
Print in Order introduces thread synchronization fundamentals. Semaphores are the cleanest tool: initialize waiting semaphores to 0 so threads block until signaled. Practice this pattern for "barrier synchronization" (all threads wait at a point), "pipeline synchronization" (stage i waits for stage i-1), and "producer-consumer" (producer signals consumer). These are the three core concurrency interview patterns.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Traffic Light Controlled Intersection | Easy | Solve | |
| The Dining Philosophers | Medium | Solve | |
| Building H2O | Medium | Solve | |
| Design Bounded Blocking Queue | Medium | Solve | |
| Fizz Buzz Multithreaded | Medium | Solve |