Quant Memo
Coding/●●●●●

Longest window with at most k distinct symbols

You have the sequence of symbols traded on a venue, one per tick. Find the length of the longest contiguous stretch containing at most k distinct symbols.

symbols = ["A", "A", "B", "A", "C", "C", "B"], k = 2
-> 4            # the stretch A A B A uses only {A, B}

Return the maximum window length. Aim for O(n)O(n).

Show a hint

If the window [left, right] violates the constraint, does growing right ever fix it?

Your answer

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

More Coding questions