Building H₂O

Difficulty: MEDIUMCategory: LLDFrequency: 1Asked On 2025-06-11

Problem Statement

Designing a system to simulate the formation of water molecules (H₂O) using hydrogen and oxygen atoms.


The system must ensure thread-safe coordination between hydrogen and oxygen threads such that:


•2 hydrogen atoms and 1 oxygen atom combine to form 1 water molecule.
•No hydrogen or oxygen atom should be wasted.


✅ Functional Requirements


1.Atom Methods
•hydrogen(releaseHydrogen): Method called by a hydrogen thread.
•oxygen(releaseOxygen): Method called by an oxygen thread.
2.Combination Rules
•Exactly 2 hydrogen threads and 1 oxygen thread can proceed to form a water molecule.
•Threads should wait if the required counterpart atoms are not yet available.
3.Synchronization
•Ensure no race conditions: hydrogen and oxygen threads must coordinate properly.
•Once a water molecule is formed, all participating threads should be released.
4.Thread Safety
•Multiple hydrogen and oxygen threads may arrive concurrently.
•Use appropriate concurrency primitives (mutex, semaphore, condition variable).


🔐 Constraints & Assumptions


•Focus is on LLD, concurrency, and correct synchronization.
•Each hydrogen or oxygen call represents a single atom/thread.
•No partial water molecules are allowed.


🧱 Key Components & Classes


1.H2O Class
1 2class H2O { 3private: 4 int hydrogenCount; 5 mutex mtx; 6 condition_variable cvHydrogen; 7 condition_variable cvOxygen; 8 9public: 10 H2O() : hydrogenCount(0) {} 11 12 void hydrogen(function<void()> releaseHydrogen) { 13 unique_lock<mutex> lock(mtx); 14 cvHydrogen.wait(lock, [&]{ return hydrogenCount < 2; }); 15 16 // release hydrogen atom 17 releaseHydrogen(); 18 hydrogenCount++; 19 20 if (hydrogenCount == 2) cvOxygen.notify_one(); // ready for oxygen 21 else cvHydrogen.notify_one(); 22 } 23 24 void oxygen(function<void()> releaseOxygen) { 25 unique_lock<mutex> lock(mtx); 26 cvOxygen.wait(lock, [&]{ return hydrogenCount == 2; }); 27 28 // release oxygen atom 29 releaseOxygen(); 30 hydrogenCount = 0; // reset for next molecule 31 cvHydrogen.notify_all(); // allow next hydrogens 32 } 33};


🔄 Example Workflow


1.Hydrogen thread calls hydrogen() → waits if 2 hydrogen atoms already present.
2.Oxygen thread calls oxygen() → waits until 2 hydrogen atoms are ready.
3.Once 2 hydrogen and 1 oxygen threads are ready → release atoms → molecule formed.
4.Repeat for the next set of hydrogen and oxygen threads.


🧪 Expected Deliverables


•Class diagram showing H2O class and its synchronization primitives.
•Implementation of hydrogen and oxygen methods.
•Test cases:
•Multiple hydrogen and oxygen threads arriving in arbitrary order
•Ensure no partial water molecules are formed
•Correct coordination under high concurrency


💡 Discussion Points / Trade-offs


1.Synchronization Strategy
•Semaphores vs condition variables
•Avoid deadlocks and starvation
2.Fairness
•Ensure no thread is starved waiting for its counterpart atoms.
3.Extensibility
•Extend to simulate other molecules (e.g., CO₂ with different ratios)


This problem tests LLD, concurrency control, thread-safe synchronization, and coordination, making it ideal for an LLD + multithreading interview round.

Solution

Comments