Drill

ProblemsTypeScript › network

Peak request burst in a sliding window

mediumnetworkTypeScript

A rate limiter must know the worst-case burst: the most requests that ever land inside a fixed-size time window.

burstWindow(timestamps: list<int>, windowSeconds: int) → int

Solve it in the editor →

Where you start

function burstWindow(timestamps: number[], windowSeconds: number): number {
  
}

Worked examples

CallResult
burstWindow([1,2,5,8,10], 5)3
burstWindow([0,10,20,30], 15)2
burstWindow([100], 10)1
burstWindow([], 5)0

Hint

Brute-force every starting index and count forward; the list is sorted, so stop at the first timestamp outside the window.

Reference solution in TypeScript
function burstWindow(timestamps: number[], windowSeconds: number): number {
  if (windowSeconds <= 0) return 0;
  let best = 0;
  for (let i = 0; i < timestamps.length; i++) {
    let count = 0;
    for (let j = i; j < timestamps.length; j++) {
      if (timestamps[j] < timestamps[i] + windowSeconds) count++;
      else break;
    }
    if (count > best) best = count;
  }
  return best;
}

The same problem in another language

More network problems in TypeScript