Quant Memo
Coding/●●●●

Spot a repeated order id within a recent window

A feed streams order ids. A duplicate id is only a problem if it recurs soon, within k messages of its earlier appearance (later repeats are legitimate reuse). You want to flag whether any such near-duplicate exists.

ids = [1, 2, 3, 1], k = 3
-> True           # the two 1s are 3 positions apart, and 3 <= k

ids = [1, 2, 3, 1], k = 2
-> False          # the two 1s are 3 apart, but 3 > k

Return True if some value repeats within k positions, else False. Aim for O(n)O(n).

Show a hint

You do not need every past occurrence of a value, only its most recent one. What is the distance to that?

Your answer

This one is open-ended. Work it through, then check your reasoning against the full solution.

More Coding questions