Skip to content

ORIGINAL CODING QUESTION

Rolling request admission

Process nondecreasing request timestamps. Admit a request only if fewer than limit previously ADMITTED requests lie in (time - window, time]. Rejected requests do not consume capacity. Equal timestamps are distinct requests. Return the admitted count.

Open practice workspace

Original exercise · medium. Editorial role relevance does not imply a company has asked this question.

Input and output

n window limit, then n timestamps.

Constraints

1 ≤ n ≤ 1000; 1 ≤ window,limit ≤ 1000; timestamps in [0,10000].

Example 1

Input

5 3 2 0 1 2 3 4

Output

4

Times 0, 1, 3 and 4 are admitted.

Example 2

Input

4 5 1 0 0 5 5

Output

2

One request at each boundary is admitted.

Explain your approach

Explain your time and auxiliary space complexity. Which input makes your algorithm work hardest?

Private grading inputs, expected outputs and canonical reference implementations are withheld. Authored expected outputs were cross-checked with independent reference oracles; compiler/runtime behavior still needs a verified execution provider.

Hint 1

Evict timestamps at or before time minus window.

Hint 2

Count admitted requests only.

Hint 3

Use a queue with a moving head.

Practice this question

Save your work privately, explain decisions and return to your revision queue.

Open practice workspaceBrowse the question library →