Print in Order - Problem

Suppose we have a class with three methods that print specific messages:

public class Foo {
  public void first() { print("first"); }
  public void second() { print("second"); }
  public void third() { print("third"); }
}

The same instance of Foo will be passed to three different threads:

  • Thread A will call first()
  • Thread B will call second()
  • Thread C will call third()

Design a mechanism to ensure that second() is executed after first(), and third() is executed after second(), regardless of thread scheduling order.

Note: We do not know how the threads will be scheduled by the operating system. The input format ensures comprehensive testing of your synchronization mechanism.

Input & Output

Example 1 — Standard Execution
$ Input: n = 1
Output: firstsecondthird
💡 Note: Thread A executes first(), signals Thread B to execute second(), which then signals Thread C to execute third(). The output is always in the correct order regardless of thread scheduling.
Example 2 — Different Thread Start Order
$ Input: n = 1
Output: firstsecondthird
💡 Note: Even if Thread C starts first, it must wait for Thread B. Thread B must wait for Thread A. The synchronization ensures the output is always firstsecondthird.
Example 3 — Concurrent Start
$ Input: n = 1
Output: firstsecondthird
💡 Note: All threads start simultaneously, but synchronization primitives ensure first() completes before second(), and second() before third().

Constraints

  • The same instance of Foo will be passed to three different threads
  • Thread A will call first()
  • Thread B will call second()
  • Thread C will call third()
  • We cannot control thread scheduling order

Visualization

Tap to expand
Print in Order - Semaphore Synchronization INPUT Three Threads (Random Order) Thread A first() Thread B second() Thread C third() Threads may execute in ANY order! Input: n = 1 Execute once per thread Need: Synchronization S1 S2 ALGORITHM STEPS 1 Initialize Semaphores sem1 = 0, sem2 = 0 (both locked initially) 2 first() executes Print "first" Signal sem1 (release) 3 second() executes Wait on sem1 (blocks) Print "second", signal sem2 4 third() executes Wait on sem2 (blocks) Print "third" first() second() third() Guaranteed execution order FINAL RESULT Synchronized Execution t=1 first() "first" t=2 second() "second" t=3 third() "third" Output: "firstsecondthird" OK - Correct! Key Insight: Semaphore Synchronization Semaphores act as gates: sem1 blocks second() until first() signals it, sem2 blocks third() until second() signals it. Initial count=0 means "locked". signal() increments (+1), wait() decrements (-1). This guarantees order regardless of OS thread scheduling. Time Complexity: O(1), Space: O(1) TutorialsPoint - Print in Order | Semaphore Synchronization Approach
Asked in
Google 35 Amazon 28 Microsoft 22 Meta 18
28.5K Views
Medium Frequency
~15 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