The "Building H2O interview question" is a classic concurrency and synchronization problem. You are given two types of threads: Hydrogen and Oxygen. These threads call methods hydrogen() and oxygen() respectively. Your goal is to synchronize these threads such that for every three threads, exactly two hydrogen threads and one oxygen thread proceed to form a water molecule (). The molecules must be formed one at a time.
Top firms like Tesla and LinkedIn use the "Building H2O coding problem" to test a candidate's knowledge of Concurrency. It evaluates your ability to use synchronization primitives like Semaphores, Barriers, or Condition Variables. It’s a test of how you handle race conditions and ensure that a specific ratio of actions is maintained in a multi-threaded environment.
The most common pattern used is Semaphores or Locks with Condition Variables.
Hydrogen thread waits for an H-slot, performs its action, and then signals that it is done.Oxygen thread waits for an O-slot, performs its action, and signals.Practice using Semaphore and CyclicBarrier in Java or threading.Semaphore in Python. Understanding the difference between a lock (mutex) and a semaphore is crucial for "Concurrency interview pattern" questions.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Design Bounded Blocking Queue | Medium | Solve | |
| Fizz Buzz Multithreaded | Medium | Solve | |
| Print Zero Even Odd | Medium | Solve | |
| The Dining Philosophers | Medium | Solve | |
| Traffic Light Controlled Intersection | Easy | Solve |