API Rate Limit Violation Detection
Updated
Problem statement
A system gives many users access to an API. To stop overuse, it limits how many requests each user can make within a sliding time window.
You are given a string array users and an integer array timestamps of the same length. Request i was made by user users[i] at time timestamps[i]. You are also given an integer limit, the largest number of requests a user may make within the window, and an integer windowSize, the length of the window. Find every user who breaks the rate limit.
The rules:
- The requests may not be in time order.
- Several requests from the same user at exactly the same timestamp count as one request.
- For a request at time
t, the window holds that user's requests whose timexsatisfiest - windowSize < x <= t. This includes the request attitself. - If that window holds more than
limitrequests, the user breaks the rate limit at timet. - For each such user, give only the first violation: the smallest time
tat which the user breaks the limit.
Return a string array with one entry "username timestamp" for each user who breaks the limit, where timestamp is that user's first violation, for example "asha 9". Sort the entries by username in ascending lexicographic order. Usernames are compared as plain strings, one character at a time, so "u10" comes before "u2". If no user breaks the limit, return ["None"].
Function
findRateLimitViolations(users: String[], timestamps: int[], limit: int, windowSize: int) → List<String>Examples
Example 1
users = ["asha","ravi","asha","ravi","asha","ravi","asha","ravi","asha"]timestamps = [20,30,1,0,9,20,4,10,8]limit = 3windowSize = 10return = ["asha 9"]In time order, asha's requests are at 1, 4, 8, 9, 20. At time 9, the window -1 < x <= 9 holds 1, 4, 8, 9. That is 4 requests, more than the limit of 3, and no earlier time had more than 3. So asha's first violation is at time 9.
ravi's requests are at 0, 10, 20, 30. At time 10, the window is 0 < x <= 10, so the request at 0 is already outside it. Every window holds only 1 of ravi's requests, so ravi never breaks the limit.
Example 2
users = ["u2","u10","u3","u2","u10","u3","u2","u10","u3"]timestamps = [5,7,0,6,12,10,5,16,20]limit = 2windowSize = 10return = ["u10 16"]u2 sends requests at 5, 6, 5. The two requests at 5 count as one, so u2 has only 2 requests, which does not exceed the limit of 2.
u3's requests are at 0, 10, 20. Each window of length 10 ending at one of them holds only that one request. For example, the window at 10 is 0 < x <= 10 and leaves out the request at 0.
u10's requests are at 7, 12, 16. At time 12, the window holds 7, 12, which is 2 requests and still allowed. At time 16, the window 6 < x <= 16 holds 7, 12, 16. That is 3 requests, more than 2, so u10's first violation is at time 16.
Example 3
users = ["user1","user2","user1","user2","user1","user2","user1"]timestamps = [1,2,3,4,5,6,7]limit = 2windowSize = 10return = ["user1 5","user2 6"]user1 sends at 1, 3, 5, 7. All of them are less than 10 apart, so at time 5 the window already holds 1, 3, 5. That is 3 requests, more than 2, and the first violation is at 5.
user2 sends at 2, 4, 6. At time 6 the window holds all three, so the first violation is at 6.
"user1" comes before "user2", so the answer is ["user1 5", "user2 6"].
Constraints
1 <= users.length <= 5000timestamps.length == users.length1 <= users[i].length <= 10users[i]consists of lowercase English letters and digits.0 <= timestamps[i] <= 10^91 <= limit <= 50001 <= windowSize <= 10^9
More Tekion interview questions
- Longest Balanced Substring After One SwapOA · Seen Jul 2026
- Peak Resource Usage Across Weighted IntervalsPHONE SCREEN · Seen Sep 2026
- Largest Rectangle in HistogramONSITE INTERVIEW · Seen Sep 2026
- Next Greater Element to the RightONSITE INTERVIEW · Seen Sep 2026
- Dungeon GamePHONE SCREEN · Seen Sep 2026
- IPOPHONE SCREEN · Seen Sep 2026
- Coin Game From The EndsPHONE SCREEN · Seen Sep 2026
- Open Lockers After Toggle PassesPHONE SCREEN · Seen Sep 2026
- Koko Eating BananasONSITE INTERVIEW · Seen Sep 2026
- Maximum Concurrent Active LoansPHONE SCREEN · Seen Aug 2026
Similar questions at other companies
- Count Servers With No Requests in a Time WindowIBM · OAIBM interview questions
- Earliest Hour with Frequent Badge AccessIntuit · OAIntuit interview questions
- Count Zero Request ServersSalesforce · ONSITE INTERVIEWSalesforce interview questions
- Sliding-Window Frequency SnapshotsGoogle · ONSITE INTERVIEWGoogle interview questions
- Detect Security Attacks from Network LogsFireworks AI · PHONE SCREEN
- Smallest Range Covering Elements from K ListsSnap Inc. · PHONE SCREENSnap Inc. interview questions
- Top K Frequent ElementsAmazon · OA · PHONE SCREEN · ONSITE INTERVIEW
- Count Belt Sections With K Fruit Pairs (Forward Deployed Engineer)Boston Consulting Group · OABoston Consulting Group interview questions
- Maximum Escape Game ScoreMicrosoft · OAMicrosoft interview questions
- Minimum Distinct Prefix CostChubb · OAChubb interview questions