Dataford
Interview QuestionsInterview GuidesExperiencesCoaching
New
Pricing
Get started
Dataford
Popular roles
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Browse
Browse All RolesEvery role hub, from analyst to MLBrowse All CompaniesCompany-specific interview loopsAll Interview GuidesThe full guide library
Top questions by role
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Top questions by skill
SQLPythonStatisticsMachine LearningA/B TestingSystem DesignGenerative AIProduct SenseMetricsBehavioral
Browse all questions →Try a mock interview
Experiences
Practice
Mock InterviewsTimed interview simulations with feedbackCoaching1:1 coaching with people at your target companiesSuccess PathYour 6-week structured planModulesCurated lessons by topicWebinarsTalks from ex-Big Tech data leadsPlaygroundA free-form scratch editor
Learn
BlogInterview strategy and career adviceTech Job Market ReportHiring trends across data and AI rolesFor UniversitiesDataford for career centersAbout DatafordWho we are and how we build
Pricing
Build my plan
Two-Thread Even Odd Print
00:00
5 left

Two-Thread Even Odd Print

HardPython

Problem

Using 2 threads, one printing odd and one printing even numbers, print 1-200 in order (busy wait and wait/notify)

Asked in the Round 2 stage. Implement a function that returns the ordered sequence produced by the two threads, once using busy waiting and once using wait/notify style synchronization.

Function contract

def print_numbers(limit):

  • Input: limit, an integer upper bound.
  • Output: a dictionary with two keys, busy_wait and wait_notify, each containing the list of numbers printed by that strategy.

Requirements

  1. Use exactly two logical workers, one responsible for odd numbers and one for even numbers.
  2. The output must contain every integer from 1 to limit exactly once, in increasing order.
  3. Provide both a busy-wait approach and a wait/notify approach in the solution.
  4. The implementation should avoid deadlock and terminate cleanly.

Constraints

  • 1 <= limit <= 200
  • Exactly two logical workers must be used, one odd and one even
  • The output must contain each integer from 1 to limit exactly once
  • The implementation must terminate without deadlock

Function Signature

def print_numbers(limit):
Interviewer

Your question is Two-Thread Even Odd Print. Start with the requirements in the Question tab.

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.
CodePython 3
Sign up free to run your codeLog inLn 2
Run your code to see test output here.