Print Zero Even Odd - Problem

Imagine you're conducting a synchronized orchestra of threads! ๐ŸŽผ

You have a function printNumber that prints integers to the console. Your task is to implement a ZeroEvenOdd class that coordinates three different threads to produce a specific pattern.

The Challenge: Three threads must work together:

  • Thread A: Calls zero() - outputs only zeros
  • Thread B: Calls even() - outputs only even numbers
  • Thread C: Calls odd() - outputs only odd numbers

The threads must coordinate to produce the sequence: "010203040506..."

Pattern Breakdown: For n=3, output should be 010203 (length 2n=6)
- Position 1,3,5: zeros (0)
- Position 2: odd number (1)
- Position 4: even number (2)
- Position 6: odd number (3)

Key Challenge: Ensure proper synchronization so threads don't interfere with each other!

Input & Output

example_1.py โ€” Basic Case
$ Input: n = 2
โ€บ Output: 0102
๐Ÿ’ก Note: For n=2, we need to print numbers 1,2. Pattern: zero(0) โ†’ odd(1) โ†’ zero(0) โ†’ even(2). Total length = 4 = 2*n.
example_2.py โ€” Medium Case
$ Input: n = 5
โ€บ Output: 0102030405
๐Ÿ’ก Note: For n=5, pattern alternates: 0-1-0-2-0-3-0-4-0-5. Zero appears before each number from 1 to 5. Total length = 10 = 2*n.
example_3.py โ€” Single Number
$ Input: n = 1
โ€บ Output: 01
๐Ÿ’ก Note: Edge case: Only number 1 to print. Pattern: zero(0) โ†’ odd(1). Total length = 2 = 2*n.

Visualization

Tap to expand
๐ŸŽผ Thread Orchestra SynchronizationSemaphore ControllerZero0Always first๐ŸŽต ReadyOdd1,3,5Waits for cue๐Ÿ˜ด WaitingEven2,4,6Waits for turn๐Ÿ˜ด WaitingpermitblockedblockedMusical Performance:0Zero plays1Odd plays02Even plays0โ™ช โ™ซ โ™ช๐ŸŽฏ Key Insight:Semaphores act like a conductor's baton - only one thread can "hold"the baton at a time, ensuring perfect synchronization without busy waiting!
Understanding the Visualization
1
Setup Phase
Zero thread gets the conductor's baton (semaphore permit), others wait
2
Zero Plays
Zero thread prints '0', then passes baton to odd or even thread
3
Number Plays
Odd/even thread prints their number, passes baton back to zero
4
Perfect Rhythm
Pattern continues: 0-1-0-2-0-3... with perfect synchronization
Key Takeaway
๐ŸŽฏ Key Insight: Semaphores provide the perfect synchronization mechanism - like a conductor's baton that ensures each thread performs exactly when needed, with zero CPU waste and guaranteed correctness.

Time & Space Complexity

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

Each number is printed exactly once, with efficient waiting

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

Only using semaphores and basic counters

n
2n
โœ“ Linear Space

Constraints

  • 1 โ‰ค n โ‰ค 1000
  • Three different threads will call zero(), even(), and odd() methods
  • Output must be exactly 2n characters long
  • Pattern must strictly alternate: 0 โ†’ number โ†’ 0 โ†’ number โ†’ ...
Asked in
Google 25 Amazon 18 Meta 15 Microsoft 12 Apple 8
58.2K Views
Medium Frequency
~18 min Avg. Time
1.5K 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