Problem · Sorting

VIP-Priority Fixed-Window Rate Limiter

Learn this problem
MediumIntuit logoIntuitFULLTIMEPHONE SCREEN

Problem statement

You are given a positive periodLength and a timestamp-ordered array of requests. Each request is [timestamp, userId, isVip], where isVip is 1 for a VIP request and 0 otherwise.

Timestamps are divided into fixed half-open periods [q * periodLength, (q + 1) * periodLength). At most five request occurrences may be admitted in each represented period. Every occurrence consumes one capacity slot, even when multiple requests use the same userId. Resolve every period as a finite batch: admit VIP requests first, then normal requests, while preserving arrival order within each class. Return one boolean per request in original input order indicating whether it is admitted.

Function

admitVipPriorityRequests(requests: int[][], periodLength: int) → boolean[]

Examples

Example 1

requests = [[0,101,0],[1,102,0],[2,103,1],[3,104,0],[4,105,1],[5,106,0],[6,107,1]]periodLength = 10return = [true,true,true,false,true,false,true]

The three VIP requests are admitted first. The first two normal requests fill the remaining capacity, so the later normal requests are rejected.

Example 2

requests = [[0,1,0],[9,2,0],[10,3,0],[19,4,1],[20,5,0],[20,6,1]]periodLength = 10return = [true,true,true,true,true,true]

The requests occupy three periods, and each period has its own five-request capacity.

Example 3

requests = [[3,1,0],[3,2,0],[3,3,0],[3,4,0],[3,5,0],[3,6,1]]periodLength = 5return = [true,true,true,true,false,true]

The VIP request takes one slot even though it arrives last. The first four normal requests keep their stable order.

Constraints

  • 1 <= periodLength <= 10^9.
  • 0 <= requests.length <= 2 * 10^5.
  • Every request contains exactly three integers: a timestamp, a user identifier, and a VIP flag.
  • Timestamps are in [0, 10^9] and are nondecreasing.
  • User identifiers are signed 32-bit integers, and every VIP flag is 0 or 1.
  • Each request occurrence consumes one capacity slot, including repeated user identifiers.

More Intuit problems

drafts saved locally
public boolean[] admitVipPriorityRequests(int[][] requests, int periodLength) {
  // write your code here
}
requests[[0,101,0],[1,102,0],[2,103,1],[3,104,0],[4,105,1],[5,106,0],[6,107,1]]
periodLength10
expected[true,true,true,false,true,false,true]
checking account