Print Numbers in Turn with Three Threads
Updated
Problem statement
Three threads, T0, T1 and T2, must print the numbers 0, 1, 2, ..., n - 1 together, taking turns in a fixed order: T0 prints 0, T1 prints 1, T2 prints 2, then T0 prints 3, and so on. So number i is always printed by thread T(i mod 3).
Use three real threads (or goroutines in Go; in JavaScript, worker threads, where the main thread may be one of the three) and coordinate them so the numbers come out in this exact order, no matter which thread the system runs first. Each thread must print only its own numbers. Building the whole answer in one thread does not solve the task.
Instead of writing to the console, each print adds the line "T<id> -> <number>" to one shared list, for example "T1 -> 4". Return that list after all three threads have finished.
Function
threeThreadPrint(n: int) → String[]Examples
Example 1
n = 5return = ["T0 -> 0","T1 -> 1","T2 -> 2","T0 -> 3","T1 -> 4"]The turns go T0, T1, T2, then back to T0 and T1, printing 0 to 4.
Example 2
n = 1return = ["T0 -> 0"]Only T0 has a number to print. T1 and T2 print nothing, but they still have to stop.
Example 3
n = 0return = []No numbers are printed, so the list is empty. All three threads must still finish.
Constraints
0 <= n <= 300- When
n = 0, nothing is printed and the list is empty, but the threads must still finish.
More The D.E. Shaw Group interview questions
- IPOPHONE SCREEN · Seen Nov 2026
- Subarray Product Less Than KPHONE SCREEN · Seen Nov 2026
- Koko Eating BananasPHONE SCREEN · Seen Nov 2026
- Sum of Smallest Non-DivisorsPHONE SCREEN · Seen Nov 2026
- Remove the Fewest Magic BeansPHONE SCREEN · Seen Oct 2026
- Equal-Weight Portfolio VariancePHONE SCREEN · Seen Oct 2026
- Count Distinct Rooted-Tree GraftsPHONE SCREEN · Seen Oct 2026
- Decode WaysPHONE SCREEN · Seen Sep 2026
- Maximum L1 Distance Between Equal-Length SubarraysPHONE SCREEN · Seen Jul 2026
- Count Lucky TicketsOA · Seen Nov 2026
Similar questions at other companies
- Timer Job Scheduler Wake-upsRedNote · PHONE SCREEN
- Detect a Deadlock from Lock LogsHelm.ai · PHONE SCREEN
- Crawl Same-Hostname URLs ConcurrentlyAnthropic · PHONE SCREENAnthropic interview questions
- Rank-Based Cache with a Data SourceLinkedIn · PHONE SCREENLinkedIn interview questions
- Parallel String Counts with a Custom Hash TableNVIDIA · PHONE SCREENNVIDIA interview questions
- Concurrent Discounted Card PurchasesBrex · PHONE SCREEN
- Concurrent Token-Bucket Rate LimiterCharta Health · PHONE SCREENCharta Health interview questions
- Ordered Number Printing with Three ThreadsArize AI · PHONE SCREEN
- Schedule Buffered Video PlaybackNuro · PHONE SCREENNuro interview questions
- Simulate a Queued Multi-Rule Rate LimiterSnowflake · PHONE SCREENSnowflake interview questions