# Sliding Window Max Unique (Practice 5023): Brief

Interviewers use this pattern to test whether you can state the invariant before coding. Here is a compact rehearsal note.

## Approach

expand/shrink with counts.

```python
def longest_k(s,k):
    from collections import Counter
    c=Counter(); i=best=0
    for j,ch in enumerate(s):
        c[ch]+=1
        while len(c)>k:
            c[s[i]]-=1
            if c[s[i]]==0: del c[s[i]]
            i+=1
        best=max(best,j-i+1)
    return best
```

## Complexity

Linear in the dominant input size for the scan; heap/tree variants add a log factor.

## Interview pitfalls

State the invariant out loud, name the complexity, then code. Skipping the invariant is the usual fail mode.

## Practicing out loud

Narrate the approach, complexity and one edge case in under a minute. Disclosure: I work on Linkjob AI, a real-time interview assistant. If you want live practice help, see [best AI tool for coding tests and OAs](https://www.linkjob.ai/).