Customer Service Queue with Timeouts
Innovaccer plays a crucial role in enhancing healthcare operations by providing robust data platforms. A common challenge in managing healthcare services, such as patient support or provider query systems, is ensuring timely responses to incoming requests while adhering to strict service level agreements (SLAs). Requests that wait too long become stale and can negatively impact patient care or operational efficiency.
This problem challenges you to design a CustomerServiceQueue that simulates a simplified support system. The queue must prioritize requests on a first-in, first-out (FIFO) basis, but crucially, it must also automatically discard any requests that have exceeded their allotted timeoutDuration at the time of processing. This ensures that only actionable, non-stale requests are ever processed.
Your task is to implement the CustomerServiceQueue class with the following methods:
constructor(currentTime: number): Initializes the customer service queue with a starting currentTime.addRequest(requestId: number, arrivalTime: number, timeoutDuration: number): void: Adds a new customer service request to the queue. Each request has a unique requestId, an arrivalTime, and a timeoutDuration after which it becomes invalid.processNextValidRequest(currentTime: number): number | null: Attempts to process the next valid (non-timed-out) request from the queue. It should return the requestId of the processed request. If the queue is empty or all pending requests have timed out, it should return null.CustomerServiceQueue(currentTime: number)currentTime: An integer representing the initial time when the queue system starts.addRequest(requestId: number, arrivalTime: number, timeoutDuration: number): voidrequestId: A unique positive integer identifying the request.arrivalTime: An integer representing the time at which the request arrived in the system.timeoutDuration: A non-negative integer representing the maximum time (in units) a request can wait before being considered timed out. A request r is timed out at current_time if r.arrivalTime + r.timeoutDuration <= current_time.voidprocessNextValidRequest(currentTime: number): number | nullcurrentTime: An integer representing the current time when this method is called.requestId (integer) of the processed request, or null if no valid request could be processed.currentTime parameter passed to constructor and processNextValidRequest will always be non-decreasing.timeoutDuration is 0, a request is considered timed out if currentTime is equal to or greater than its arrivalTime when processNextValidRequest is called.processNextValidRequest is called, you must first discard any requests at the front of the queue that have already timed out based on the provided currentTime, before attempting to process the next valid one.Examples
Example 1
Input:
operations = ["CustomerServiceQueue","addRequest","addRequest","processNextValidRequest","addRequest","processNextValidRequest","processNextValidRequest"], params = [[0],[101,5,10],[102,7,5],[15],[103,12,3],[16],[20]]
Output:
[null,null,null,101,null,null,null]
Note:
Example 2
Input:
operations = ["CustomerServiceQueue","addRequest","addRequest","processNextValidRequest","processNextValidRequest","addRequest","processNextValidRequest"], params = [[100],[201,100,5],[202,102,0],[105],[105],[203,106,10],[110]]
Output:
[null,null,null,201,null,null,203]
Note:
Example 3
Input:
operations = ["CustomerServiceQueue","addRequest","addRequest","addRequest","processNextValidRequest","processNextValidRequest"], params = [[10],[301,10,2],[302,11,1],[303,12,0],[13],[13]]
Output:
[null,null,null,null,null,null]
Note:
Constraints
1 <= requestId <= 10^90 <= arrivalTime, timeoutDuration, currentTime <= 10^9currentTime passed to processNextValidRequest will always be >= the currentTime used in the constructor and any arrivalTime of added requests.addRequest and processNextValidRequest will not exceed 10^5.requestIds are unique.addRequest and processNextValidRequest operate efficiently, ideally in O(1) amortized time for addRequest and O(k) where k is the number of timed-out requests discarded for processNextValidRequest.Loading editor...