Coordinate Three Threads to Print an Ordered Sequence

Read the full interview experience this question came from →

Quick Overview

Coordinate ZeroThread, OddThread, and EvenThread to print a labeled sequence from zero through a limit in strict order. The solution explains condition predicates, spurious wakeups, atomic state changes, termination, exception-safe notification, and deterministic concurrency testing.

Coordinate Three Threads to Print an Ordered Sequence

Company: Arize

Role: Software Engineer

Category: Software Engineering Fundamentals

Difficulty: medium

Interview Round: Technical Screen

# Coordinate Three Threads to Print an Ordered Sequence Given a nonnegative integer `n`, use exactly three independent thread classes to print every integer from 0 through `n` in strictly increasing order. - `ZeroThread` owns the logic that prints 0, and 0 is printed exactly once. - `OddThread` owns the logic that prints positive odd integers. - `EvenThread` owns the logic that prints positive even integers. Each output line includes both the number and the responsible thread, for example `3 printed by OddThread`. The printing logic must remain inside the corresponding thread class. Explain and provide language-appropriate pseudocode for the shared coordination object and all three thread loops. Handle spurious wakeups, termination, and exceptions without deadlocking another thread. ### Clarifying Questions to Ask - Does the required language provide condition variables, semaphores, or both? - Must the method return captured output for testing, or write to an injected printer? - If printing throws, should the other threads stop immediately? - May `n` be zero? ### What a Strong Answer Covers - A shared next-number state protected by one synchronization discipline. - Predicate-based waiting in a loop, not reliance on timing or a single unchecked wakeup. - Exactly one owner for each printed value and an atomic print-and-advance transition. - Wakeup and termination behavior for `n = 0`, normal completion, spurious wakeups, interruption, and print failure. - A deterministic test strategy that runs the real threads repeatedly and validates the complete labeled sequence. ### Follow-up Questions 1. How would the design change to support `p` worker threads where worker `i` prints values congruent to `i mod p`? 2. Can semaphores remove the shared condition predicate, and what shutdown problem remains? 3. How would you test for a rare missed notification without using arbitrary sleeps?

Overview: Coordinate ZeroThread, OddThread, and EvenThread to print a labeled sequence from zero through a limit in strict order. The solution explains condition predicates, spurious wakeups, atomic state changes, termination, exception-safe notification, and deterministic concurrency testing.

Read the full Arize Software Engineer interview experience this question came from

|Home/Software Engineering Fundamentals/Arize
Arize logo
Arize
Aug 31, 2026
mediumSoftware EngineerTechnical ScreenSoftware Engineering Fundamentals
0
0

Coordinate Three Threads to Print an Ordered Sequence

Given a nonnegative integer n, use exactly three independent thread classes to print every integer from 0 through n in strictly increasing order.

  • ZeroThread owns the logic that prints 0, and 0 is printed exactly once.
  • OddThread owns the logic that prints positive odd integers.
  • EvenThread owns the logic that prints positive even integers.

Each output line includes both the number and the responsible thread, for example 3 printed by OddThread. The printing logic must remain inside the corresponding thread class.

Explain and provide language-appropriate pseudocode for the shared coordination object and all three thread loops. Handle spurious wakeups, termination, and exceptions without deadlocking another thread.

Clarifying Questions to Ask Guidance

  • Does the required language provide condition variables, semaphores, or both?
  • Must the method return captured output for testing, or write to an injected printer?
  • If printing throws, should the other threads stop immediately?
  • May n be zero?

What a Strong Answer Covers Guidance

  • A shared next-number state protected by one synchronization discipline.
  • Predicate-based waiting in a loop, not reliance on timing or a single unchecked wakeup.
  • Exactly one owner for each printed value and an atomic print-and-advance transition.
  • Wakeup and termination behavior for n = 0 , normal completion, spurious wakeups, interruption, and print failure.
  • A deterministic test strategy that runs the real threads repeatedly and validates the complete labeled sequence.

Follow-up Questions Guidance

  1. How would the design change to support p worker threads where worker i prints values congruent to i mod p ?
  2. Can semaphores remove the shared condition predicate, and what shutdown problem remains?
  3. How would you test for a rare missed notification without using arbitrary sleeps?
Loading comments...