The Print Zero Even Odd concurrency problem requires four threads to print numbers in the sequence 0,1,0,2,0,3,...,0,n using coordinated methods: zero() prints 0, even() prints even numbers, and odd() prints odd numbers. The threads must interleave correctly. This medium concurrency coding problem uses multiple semaphores for ordering. The concurrency interview pattern is demonstrated at a higher level than Print in Order.
Goldman Sachs asks this to test multi-phase semaphore coordination. Three threads must alternate in a specific pattern, and the correct synchronization prevents race conditions while ensuring the right sequence.
Three-semaphore coordination. zero_sem=1, odd_sem=0, even_sem=0. zero(): loop n times: wait zero_sem, print 0, signal odd_sem (if i odd) or even_sem (if i even). odd(): loop n//2 or ceil(n/2) times: wait odd_sem, print, signal zero_sem. even(): loop n//2 times: wait even_sem, print, signal zero_sem.
n=4. Sequence: 0,1,0,2,0,3,0,4.
Multi-semaphore concurrency problems require drawing the signal flow before coding. Draw: zero→(odd or even)→zero→... as a directed graph. Each arrow is a semaphore signal. Initialize semaphores based on which thread runs first. For n=4: 2 odd prints, 2 even prints, 5 zero prints. Practice mapping signal flows to semaphore initialization for complex ordering patterns.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Building H2O | Medium | Solve | |
| Design Bounded Blocking Queue | Medium | Solve | |
| Fizz Buzz Multithreaded | Medium | Solve | |
| The Dining Philosophers | Medium | Solve | |
| Print in Order | Easy | Solve |