Frontend School

Batch API Request Processor

DSA
medium

Batch API Request Processor

Medium
Queue
Design
System Design Basics

Overview

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.

What you need to do / Task

Implement the BatchRequestProcessor class with the following methods:

  1. constructor(maxRequestsPerCycle: number): Initializes the processor with a maximum number of requests it can process before needing a flush operation.
  2. 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.
  3. 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.
  4. processNextRequest(): Request | null: Retrieves and marks the next available request for processing. It must adhere to the following rules:
    • Always prioritize requests from the urgent queue. If the urgent queue is empty, then process from the regular queue.
    • If the maxRequestsPerCycle limit has been reached since the last flush, this method should return null until flush() is called.
    • If no requests are pending in either queue and the limit hasn't been reached, it should return null.
    • Each successful call to this method increments an internal counter for the current cycle.
  5. 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.

Input & output

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.
  • Returns: void (constructor).

addRegularBatch(requests: Request[]): void

  • requests: An array of Request objects to be added to the regular processing queue.
  • Returns: void.

addUrgentBatch(requests: Request[]): void

  • requests: An array of Request objects to be added to the urgent processing queue.
  • Returns: void.

processNextRequest(): Request | null

  • Returns: The next Request object to be processed, or null if no requests are available or the maxRequestsPerCycle limit has been reached.

flush(): void

  • Returns: void.

Clarifications

  • Empty Batches: If an empty array [] 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).
  • No Pending Requests: If both queues are empty, processNextRequest() should return null.
  • Request IDs: Request ids are unique within their respective batches but not necessarily globally unique across all batches or processing cycles.

Examples

1

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.

2

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.

3

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

Constraints & assumptions

  • maxRequestsPerCycle will be an integer between 1 and 1000.
  • The number of requests in a batch can range from 0 to 100.
  • The total number of addBatch operations will not exceed 1000.
  • The total number of processNextRequest calls will not exceed 10,000.
  • All method calls to BatchRequestProcessor will be single-threaded (no need to handle concurrency).
  • The 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.

Out of scope

  • Error handling beyond returning null as specified.
  • Persistence of queues or state across application restarts.
  • Network I/O or actual API request execution.
  • Detailed performance profiling beyond using efficient data structures for queue operations.

What interviewers evaluate

  • Correct implementation of priority logic (urgent over regular requests).
  • Accurate FIFO ordering for requests within each priority level.
  • Precise handling and enforcement of the maxRequestsPerCycle limit.
  • Effective use of flush() to reset the processing cycle.
  • Selection of appropriate data structures (e.g., standard queues) for efficient operations.
  • Code clarity, readability, and handling of edge cases (e.g., empty queues, full cycle).

Loading editor...