Building H2O - Problem

There are two kinds of threads: oxygen and hydrogen. Your goal is to group these threads to form water molecules.

There is a barrier where each thread has to wait until a complete molecule can be formed. Hydrogen and oxygen threads will be given releaseHydrogen and releaseOxygen methods respectively, which will allow them to pass the barrier.

These threads should pass the barrier in groups of three, and they must immediately bond with each other to form a water molecule. You must guarantee that all the threads from one molecule bond before any other threads from the next molecule do.

Constraints:

  • If an oxygen thread arrives at the barrier when no hydrogen threads are present, it must wait for two hydrogen threads
  • If a hydrogen thread arrives at the barrier when no other threads are present, it must wait for an oxygen thread and another hydrogen thread
  • Threads pass the barriers in complete sets of three (1 oxygen + 2 hydrogen)

Write synchronization code for oxygen and hydrogen molecules that enforces these constraints.

Input & Output

Example 1 — Basic Thread Sequence
$ Input: n = 3 threads: H, H, O
Output: All threads form one H2O molecule
💡 Note: Two hydrogen threads and one oxygen thread arrive, they coordinate through semaphores and all proceed together to form a water molecule.
Example 2 — Oxygen Arrives First
$ Input: n = 3 threads: O, H, H
Output: Oxygen waits, then all form H2O molecule
💡 Note: Oxygen thread arrives first but must wait for two hydrogen threads. Once both hydrogens arrive, all three proceed together.
Example 3 — Multiple Molecules
$ Input: n = 6 threads: H, H, O, H, H, O
Output: Two separate H2O molecules formed
💡 Note: First three threads (H, H, O) form one molecule, then the next three threads form a second molecule. No mixing between molecules.

Constraints

  • 1 ≤ n ≤ 1000
  • n represents total number of threads
  • Threads arrive in any order
  • Must form complete H2O molecules

Visualization

Tap to expand
Building H₂O: Thread Coordination ProblemInput ThreadsHHOThreads arrive randomlySynchronization Layer• Semaphores for coordination• Barriers for group formationOutput MoleculeHHOH₂O MoleculeKey Requirements:✓ Threads pass barrier in groups of 3 (2H + 1O)✓ Complete molecule bonds before next molecule starts✓ Threads wait efficiently (no busy waiting)
Understanding the Visualization
1
Input Threads
Hydrogen and Oxygen threads arrive in random order
2
Synchronization
Threads coordinate using semaphores and barriers
3
Output Molecules
Complete H₂O molecules formed with perfect synchronization
Key Takeaway
🎯 Key Insight: Semaphores provide elegant thread coordination by counting available resources and blocking threads until complete molecules can be formed
Asked in
Google 32 Facebook 28 Amazon 25 Microsoft 22
28.5K Views
Medium Frequency
~35 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen