# Sliding Window Max Unique (Practice 4238): Pattern
Interviewers love sliding window max unique because it checks whether you can state the invariant before coding. Here is a compact rehearsal note.
## Approach
Expand right; shrink left when a duplicate appears.
```python
def length_of_longest(s):
last = {}
best = left = 0
for i, c in enumerate(s):
if c in last and last[c] >= left: left = last[c] + 1
last[c] = i
best = max(best, i - left + 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 [https://linkjob.ai](https://linkjob.ai/).