Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Kafka-Style Queue Capacity Simulation

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

Your question is Kafka-Style Queue Capacity Simulation. 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

RBC Online Banking receives requests that require processing by a single worker. Given each request's arrival timestamp and processing duration, simulate a queue whose total capacity is four requests, including the request currently being processed.

Requests are provided in nondecreasing timestamp order. When a request arrives, remove every previously accepted request that has completed by that timestamp. If fewer than four requests remain in the system, accept the new request and schedule it after all currently queued work. Otherwise, drop it.

Return the number of accepted requests and dropped requests as [accepted, dropped].

Formal Specification

Implement process_rbc_requests(processing_times, timestamps), where both inputs are arrays of integers of equal length. processing_times[i] is the duration of request i, and timestamps[i] is its arrival time. A request completing exactly at another request's arrival timestamp is removed before the new request is considered. The worker processes requests in arrival order.

Constraints

  • 0 <= len(processing_times) = len(timestamps) <= 10^5
  • 1 <= processing_times[i] <= 10^9
  • 0 <= timestamps[i] <= 10^9
  • timestamps is sorted in nondecreasing order
  • Queue capacity is exactly four, including the active request

Function Signature

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