Batch API Request Processor
At Postman, managing and executing API requests efficiently is core to our product. Imagine a scenario where users can queue up various API requests for execution. Some of these requests might be part of a standard workflow, while others could be critical, urgent retries or diagnostics that need immediate attention. Furthermore, to prevent resource exhaustion or rate-limiting issues, there's a need to control the number of requests processed within a specific operational cycle.
This problem challenges you to design a BatchRequestProcessor that prioritizes urgent API requests, processes regular requests, and adheres to a configurable processing limit per cycle, allowing for manual 'flushing' to reset the cycle.
Implement the BatchRequestProcessor class with the following methods:
constructor(maxRequestsPerCycle: number): Initializes the processor with a maximum number of requests it can process before needing a flush operation.addRegularBatch(requests: Request[]): void: Adds a batch of regular API requests to the processing queue. Requests within this batch, and across other regular batches, should be processed in FIFO order.addUrgentBatch(requests: Request[]): void: Adds a batch of urgent API requests to a dedicated high-priority queue. Requests within this batch, and across other urgent batches, should be processed in FIFO order.processNextRequest(): Request | null: Retrieves and marks the next available request for processing. It must adhere to the following rules:
maxRequestsPerCycle limit has been reached since the last flush, this method should return null until flush() is called.null.flush(): void: Resets the internal counter for requests processed in the current cycle. This allows processNextRequest to resume processing if it was previously blocked by the maxRequestsPerCycle limit.Request Type Definition:
interface Request {
id: string; // Unique identifier for the request
payload?: any; // Optional data associated with the request
}
BatchRequestProcessor(maxRequestsPerCycle: number)
maxRequestsPerCycle: A positive integer indicating the maximum number of requests that can be processed between flush() calls.void (constructor).addRegularBatch(requests: Request[]): void
requests: An array of Request objects to be added to the regular processing queue.void.addUrgentBatch(requests: Request[]): void
requests: An array of Request objects to be added to the urgent processing queue.void.processNextRequest(): Request | null
Request object to be processed, or null if no requests are available or the maxRequestsPerCycle limit has been reached.flush(): void
void.[] is passed to addRegularBatch or addUrgentBatch, it should be handled gracefully (i.e., do nothing).maxRequestsPerCycle: You can assume maxRequestsPerCycle will always be a positive integer (> 0).processNextRequest() should return null.ids are unique within their respective batches but not necessarily globally unique across all batches or processing cycles.Examples
Example 1
Input:
operations = ["BatchRequestProcessor","addRegularBatch","addUrgentBatch","processNextRequest","processNextRequest","addUrgentBatch","processNextRequest","processNextRequest","flush","processNextRequest","processNextRequest"], params = [[3],[["R1",{"data":"reg_1"}],["R2",{"data":"reg_2"}]],[["U1",{"data":"urgent_1"}]],[],[],[["U2",{"data":"urgent_2"}]],[],[],[],[],[]]
Output:
[null,null,null,{"id":"U1","data":"urgent_1"},{"id":"R1","data":"reg_1"},null,{"id":"U2","data":"urgent_2"},null,null,{"id":"R2","data":"reg_2"},null]
Note:
Initializes with a limit of 3. U1 is processed first due to priority. R1 is next. Then a new urgent batch adds U2. U2 is processed before R2. After 3 requests (U1, R1, U2), the limit is hit. Flush resets the counter, allowing R2 to be processed.
Example 2
Input:
operations = ["BatchRequestProcessor","addRegularBatch","addRegularBatch","processNextRequest","processNextRequest","processNextRequest","processNextRequest","flush","addUrgentBatch","processNextRequest"], params = [[2],[["A"],["B"]],[["C"]],[],[],[],[],[],[["D"]],[]]
Output:
[null,null,null,{"id":"A"},{"id":"B"},null,null,null,null,{"id":"D"}]
Note:
Processor limit is 2. A and B are added. A is processed, then B. After B, the limit is reached, so subsequent processNextRequest calls return null. After flush, D (an urgent request) is added and immediately processed.
Example 3
Input:
operations = ["BatchRequestProcessor","processNextRequest","addUrgentBatch","addRegularBatch","processNextRequest","addRegularBatch","processNextRequest","processNextRequest","flush","processNextRequest"], params = [[1],[],[["X"]],[["Y"]],[],[["Z"]],[],[],[],[]]
Output:
[null,null,null,null,{"id":"X"},null,{"id":"Y"},null,null,{"id":"Z"}]
Note:
Initial limit is 1. No requests initially, so processNextRequest returns null. X (urgent) and Y (regular) are added. X is processed. Limit is hit. Z is added to regular. processNextRequest returns null until flush. After flush, Y is processed, then limit is hit again. Another flush, then Z is processed.
Constraints
maxRequestsPerCycle will be an integer between 1 and 1000.addBatch operations will not exceed 1000.processNextRequest calls will not exceed 10,000.BatchRequestProcessor will be single-threaded (no need to handle concurrency).Request object payload can be of any type and does not need to be processed or interpreted by your solution; it merely needs to be stored and returned.null as specified.maxRequestsPerCycle limit.flush() to reset the processing cycle.Loading editor...