Print in Order - Problem
Thread Synchronization Challenge: Print in Order

Imagine you're conducting an orchestra where three musicians need to play their parts in perfect sequence, but they can't see or hear each other! This classic concurrency problem asks you to coordinate multiple threads to execute methods in a specific order.

You're given a class Foo with three methods:
  • first() - prints "first"
  • second() - prints "second"
  • third() - prints "third"

The challenge: Three different threads will call these methods simultaneously, but you must ensure they execute in the correct order: first โ†’ second โ†’ third. The operating system scheduler is unpredictable, so without proper synchronization, the output could be jumbled!

Goal: Design a thread-safe mechanism to guarantee sequential execution regardless of thread scheduling.

Input & Output

example_1.py โ€” Basic Execution Order
$ Input: Thread execution order: [1,2,3]
โ€บ Output: "firstsecondthird"
๐Ÿ’ก Note: Even though threads start in order 1โ†’2โ†’3, the synchronization mechanism ensures they execute their methods in the correct sequence regardless of timing.
example_2.py โ€” Reverse Thread Start
$ Input: Thread execution order: [3,1,2]
โ€บ Output: "firstsecondthird"
๐Ÿ’ก Note: Despite thread 3 starting first, it must wait for threads 1 and 2 to complete their work. The synchronization ensures proper ordering.
example_3.py โ€” Random Thread Timing
$ Input: Thread execution order: [2,3,1]
โ€บ Output: "firstsecondthird"
๐Ÿ’ก Note: No matter how the OS schedules these threads, the output is always consistent thanks to our synchronization mechanism.

Visualization

Tap to expand
Restaurant Kitchen Coordination Analogy๐Ÿ‘จโ€๐Ÿณ Appetizer Cheffirst() methodWorks immediatelySignals when done โœ…๐Ÿ‘จโ€๐Ÿณ Main Course Chefsecond() methodWaits for appetizer signal ๐Ÿ””Then signals dessert โœ…๐Ÿ‘จโ€๐Ÿณ Dessert Chefthird() methodWaits for main course ๐Ÿ””Completes sequence โœ…Signal 1Signal 2๐Ÿ”‘ Key Insight: Efficient CoordinationโŒ Busy WaitingChef constantly checks"Is appetizer ready?"Wastes energy!โœ… SemaphoreChef sleeps untilbell rings ๐Ÿ””Maximum efficiency!๐Ÿ† Result: Perfect meal sequence with minimal wasted effort
Understanding the Visualization
1
All chefs start cooking
Three chefs begin preparation simultaneously, but main course and dessert chefs wait for signals
2
Appetizer ready!
Appetizer chef finishes and rings the bell, signaling the main course chef to serve
3
Main course served
Main course chef serves their dish and signals the dessert chef
4
Perfect sequence complete
Dessert chef receives the signal and serves the final course - meal served in perfect order!
Key Takeaway
๐ŸŽฏ Key Insight: Use synchronization primitives like semaphores to coordinate threads efficiently - no busy waiting, just clean signals when each step is complete!

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(1)

Each method executes exactly once with minimal blocking overhead

n
2n
โœ“ Linear Growth
Space Complexity
O(1)

Only requires a few semaphore objects for coordination

n
2n
โœ“ Linear Space

Constraints

  • Exactly three threads will be created
  • Each thread calls exactly one method on the same Foo instance
  • Thread scheduling order is unpredictable and not guaranteed
  • Methods must execute in exact order: first() โ†’ second() โ†’ third()
  • Solution must work regardless of OS thread scheduling
Asked in
Google 42 Meta 38 Amazon 31 Microsoft 28
89.2K Views
High Frequency
~25 min Avg. Time
1.8K 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