Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Print 1 to N with Threads

HardPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Print 1 to N with Threads. Start with the requirements on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

Given an integer n, write a function that returns the sequence of outputs produced by three coordinated threads printing numbers from 1 to n. Thread A prints numbers where num % 3 == 1, Thread B prints numbers where num % 3 == 2, and Thread C prints numbers where num % 3 == 0. The final output must be in exact order from 1 to n.

Constraints

  • 1 <= n <= 10^4
  • Exactly three worker threads participate
  • Each number from 1 to n must appear exactly once in increasing order

Function Signature

def print_sequence_three_threads(n):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output