# The Window Cleaner

> Keep it fresh, keep it unique.

Canonical URL: <https://datadriven.io/problems/the_window_cleaner>

Domain: Python · Difficulty: medium · Seniority: L4

## Problem

Given a string, return the length of the longest substring containing no repeated characters.

## Worked solution and explanation

### Why this problem exists in real interviews

This tests the **sliding window** technique for substring problems. Finding the longest substring without repeating characters is a canonical problem that probes window expansion/contraction logic and efficient character tracking.

> **Trick to Solving**
>
> Maintain a window `[left, right]` and a set (or dict) of characters in the current window. Expand right. When a duplicate is found, shrink from the left until the duplicate is removed.

---

### Break down the requirements

#### Step 1: Expand the window by advancing the right pointer

Add the new character to the tracking set.

#### Step 2: Shrink the window when a duplicate appears

Advance the left pointer, removing characters from the set, until the duplicate is gone.

#### Step 3: Track the maximum window size

After each adjustment, update the best length if the current window is larger.

---

### The solution

**Sliding window with set-based duplicate detection**

```python
def longest_unique_substring(s: str) -> int:
    char_set = set()
    left = 0
    best = 0
    for right in range(len(s)):
        while s[right] in char_set:
            char_set.remove(s[left])
            left += 1
        char_set.add(s[right])
        window_size = right - left + 1
        if window_size > best:
            best = window_size
    return best
```

> **Time and Space Complexity**
>
> **Time:** O(n). Each character is added and removed from the set at most once, giving amortized O(1) per step.
> 
> **Space:** O(min(n, k)) where k is the character set size (e.g., 26 for lowercase English, 128 for ASCII).

> **Interviewers Watch For**
>
> Using a dict mapping characters to their last seen index allows jumping the left pointer directly instead of incrementally. This is an optimization that avoids the inner while loop entirely.

> **Common Pitfall**
>
> Checking all substrings with nested loops is O(n^2) or O(n^3). The sliding window approach is O(n) and is the expected solution.

---

## Common follow-up questions

- How would you return the actual substring, not just its length? _(Tests tracking the start index of the best window.)_
- What if you allowed at most k repeating characters? _(Tests extending to the "longest substring with at most k distinct characters" variant.)_
- How would you handle unicode characters? _(Tests that the set approach works for any hashable character, including unicode.)_
- What if the input was a stream of characters? _(Tests maintaining the window state across incremental character arrivals.)_

## Related

- [All practice problems](https://datadriven.io/problems)
- [Mock interview mode](https://datadriven.io/interview/the_window_cleaner)
- [Python Interview Questions](https://datadriven.io/python-interview-questions)
- [Data Engineering Interview Prep Guide](https://datadriven.io/data-engineer-interview-prep)
- [Daily Challenge](https://datadriven.io/daily)

---

Source: DataDriven (https://datadriven.io). 100% free data engineering interview prep. Live code execution against Postgres 16, Python 3.11, and Spark sandboxes. No paywall, no premium tier, no signup gate.