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
ExperiencesCoaching
New
Practice
Mock InterviewsTimed interview simulations with feedbackSuccess 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
Get started
Thread-Safe Queue for Async Batching
00:00
5 left

Thread-Safe Queue for Async Batching

HardPython

Problem

Amazon SageMaker inference services often improve throughput by processing requests in batches. Implement a thread-safe batching queue that accepts requests from producer threads and returns FIFO batches when either the batch reaches max_batch_size or the oldest queued request has waited at least max_wait_ms.

For deterministic evaluation, implement batch_requests, which simulates timestamped producer arrivals and returns the batches that would be emitted. Your implementation must use a thread-safe queue abstraction backed by threading.Condition or an equivalent synchronization mechanism.

Formal Specification

Implement batch_requests(requests, max_batch_size, max_wait_ms). requests is a list of two-element lists [arrival_ms, request_id], sorted by nondecreasing arrival time. Return a list of batches, where each batch is a list of request IDs in FIFO order. Before adding a request arriving at time t, flush any ready batch at time t. After all requests arrive, flush remaining requests at the last arrival time plus max_wait_ms.

A batch may contain at most max_batch_size requests. Requests arriving at the exact expiration time are processed after the existing batch is flushed.

Constraints

  • 0 <= len(requests) <= 10^4
  • 1 <= max_batch_size <= 10^3
  • 0 <= max_wait_ms <= 10^6
  • Arrival times are integers in [0, 10^9]
  • Requests are sorted by nondecreasing arrival time
  • Request IDs are unique strings

Function Signature

def batch_requests(requests, max_batch_size, max_wait_ms):
Interviewer

Your question is Thread-Safe Queue for Async Batching. 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.
Code
Sign up free to run your codeLog inLn 2
Run your code to see test output here.