Drill

ProblemsJava › network

Peak request burst in a sliding window

mediumnetworkSliding windowArraysJava

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

Java needs a compiler and Drill does not host one yet, so this page is the reference rather than an exercise: the problem, worked examples, and the solution in full. To type it out, the same problem runs in Python.

Solve it in Python →

Where you start

int burstWindow(List<Integer> timestamps, int windowSeconds) {
    
}

Worked examples

CallResult
burstWindow(Main.<Integer>ls(1, 2, 5, 8, 10), 5)3
burstWindow(Main.<Integer>ls(0, 10, 20, 30), 15)2
burstWindow(Main.<Integer>ls(100), 10)1
burstWindow(Main.<Integer>ls(), 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 Java
int burstWindow(List<Integer> timestamps, int windowSeconds) {
    if (windowSeconds <= 0) return 0;
    int best = 0;
    for (int i = 0; i < timestamps.size(); i++) {
        int count = 0;
        for (int j = i; j < timestamps.size(); j++) {
            if (timestamps.get(j) < timestamps.get(i) + windowSeconds) count++;
            else break;
        }
        if (count > best) best = count;
    }
    return best;
}

The same problem in another language

More network problems in Java