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 H2O - Semaphore Coordination INPUT n = 3 threads arriving H Thread 1 H Thread 2 O Thread 3 BARRIER hydrogenSem = 2 oxygenSem = 1 Input Threads: [H, H, O] ALGORITHM STEPS 1 Initialize Semaphores H=2, O=1, barrier=0 2 H threads acquire 2 H wait on hydrogenSem 3 O thread acquires 1 O waits on oxygenSem 4 Bond and Release Form H2O, reset sems Coordination Flow: H H O WAIT for 3 H2O OK FINAL RESULT Formed Water Molecule: O H H Thread Status: H1: releaseHydrogen OK H2: releaseHydrogen OK O1: releaseOxygen OK Output: 1 H2O molecule formed All 3 threads bonded correctly Key Insight: Semaphores control thread access: hydrogenSem(2) allows exactly 2 H threads, oxygenSem(1) allows exactly 1 O thread. A barrier ensures all 3 threads arrive before bonding. After forming H2O, semaphores reset for the next molecule. This guarantees atomic molecule formation: threads from different molecules never mix during bonding. TutorialsPoint - Building H2O | Semaphore-Based Coordination
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