Design a class to measure ongoing requests per clientId
Difficulty: EASYCategory: LLDFrequency: 1Asked On 2025-03-19
Problem Statement
Designing a class that monitors ongoing (in-flight) requests for each clientId in a multi-threaded environment.
Each request is uniquely identified by a reqId, and your system should allow tracking when the request starts (processRequest) and when it finishes (processResponse). At any time, the system should be able to report the number of ongoing requests for a given client using getOngoingRequests.
🎯 Functional Requirements
Design a class with the following methods:
1. void processRequest(String reqId, String clientId)
•Called when a request is received.
•Increment the count of ongoing requests for the given clientId.
2. void processResponse(String reqId, String clientId)
•Called when the response is sent.
•Decrement the count of ongoing requests for the given clientId.
•Ensure no negative values or underflow.
3. long getOngoingRequests(String clientId)
•Returns the number of in-flight requests for the given clientId.