API Shield with Two Rolling Limits
Updated
Problem statement
Requests arrive at integer seconds. There are n requests, and t[i] is the arrival second of request i. A first-in-first-out (FIFO) buffer holds up to b waiting requests and drops the newest arrivals that exceed its capacity at a second.
Sends must satisfy both limits: at most m1 sends in any w1-second window, and at most m2 sends in any w2-second window. A w-second window is any w consecutive seconds.
Inside one second s, events happen in this order:
- All requests that arrive at second
sjoin the back of the buffer. - Requests are sent from the front of the buffer. Any number of them may be sent at second
s, as long as, counting them, secondss - w1 + 1toshold at mostm1sends and secondss - w2 + 1toshold at mostm2sends. - If more than
brequests are still waiting, the newest ones are dropped untilbremain.
Sending continues after the last arrival until the buffer is empty. Minimize the number of dropped requests, and return that smallest number.
Function
minDropped(n: int, w1: int, m1: int, w2: int, m2: int, b: int, t: int[]) → intExamples
Example 1
n = 9w1 = 1m1 = 2w2 = 3m2 = 3b = 2t = [0,0,0,0,1,1,2,3,3]return = 2Limit 1 allows at most 2 sends in any 1 second, and limit 2 allows at most 3 sends in any 3 consecutive seconds.
- Second 0: 4 requests arrive. 2 are sent (limit 1). 2 wait, which fits in the buffer (
b = 2). - Second 1: 2 arrive, so 4 are waiting. The 3-second window ending at second 1 already holds 2 sends, so limit 2 allows only 1 more. 3 still wait, so the newest one is dropped.
- Second 2: 1 arrives, so 3 are waiting. The window ending at second 2 already holds 3 sends, so nothing is sent, and the newest request (the one that just arrived) is dropped.
- Second 3: 2 arrive, so 4 are waiting. The window ending at second 3 holds only 1 send, so 2 are sent and 2 wait.
No more requests arrive, so the 2 waiting requests are sent later and nothing else is dropped. No other way of sending drops fewer than 2 requests.
Example 2
n = 6w1 = 1m1 = 2w2 = 4m2 = 3b = 0t = [1,1,1,2,2,5]return = 2With b = 0 nothing may wait, so a request is kept only if it is sent in the second it arrives.
- Second 1: 3 requests arrive. Limit 1 allows 2 sends in this second, so 2 are sent and the newest is dropped.
- Second 2: 2 arrive. The 4-second window ending at second 2 already holds 2 sends, so limit 2 allows 1 more. 1 is sent and 1 is dropped.
- Second 5: 1 arrives. The 4-second window ending at second 5 covers seconds 2 to 5 and holds 1 send, so it is sent.
2 requests are dropped.
Example 3
n = 4w1 = 1000000000m1 = 2w2 = 1m2 = 1b = 1t = [0,0,0,5]return = 1Limit 1 allows only 2 sends in any 10^9 consecutive seconds, and limit 2 allows 1 send per second.
- Second 0: 3 requests arrive. 1 is sent and 2 wait, which is more than
b = 1, so the newest is dropped. - Second 1: the waiting request is sent. That is the second send in the long window.
- Second 5: 1 arrives. The long window already holds 2 sends, so it waits. It fits in the buffer, and it is sent at second
10^9, when the send from second 0 leaves the window.
1 request is dropped.
Constraints
1 <= n <= 1000t.length == n0 <= t[i] <= 10^9tis sorted in non-decreasing order.1 <= w1, w2 <= 10^91 <= m1, m2 <= 10000 <= b <= 1000
More Graviton Online Assessment questions
Similar questions at other companies
- Incident MonitorStripe · OAStripe interview questions
- Per-User Sliding-Window Rate LimiterPayPal · OAPayPal interview questions
- Per-Client Sliding-Window Rate LimiterGeneral Motors · OA
- Key-Value Store GET and PUT QPSDatabricks · PHONE SCREENDatabricks interview questions
- Team-Preferred Stable MatchingBloomberg LP · ONSITE INTERVIEWBloomberg LP interview questions
- Three-Stage Labeling Workforce SimulationScale AI · PHONE SCREENScale AI interview questions
- Sliding-Window API Rate LimiterPatreon · PHONE SCREENPatreon interview questions
- Sliding-Window Rate LimiterSnowflake · ONSITE INTERVIEWSnowflake interview questions
- Rate Limiter Sliding Window With Per-Entity LimitsRoblox · PHONE SCREEN · ONSITE INTERVIEWRoblox interview questions
- Metric Counter with a Fake ClockSnap Inc. · ONSITE INTERVIEWSnap Inc. interview questions