# The Streak Breaker

> It has a problem with repetition.

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

Domain: Python · Difficulty: easy · Seniority: L3

## Problem

Given a list, return a new list where consecutive duplicates are collapsed to a single element (only the first of each run is kept). Non-consecutive duplicates are preserved.

## Worked solution and explanation

### Why this problem exists in real interviews

This tests **consecutive duplicate removal**, a common data cleaning pattern. It probes whether a candidate understands the difference between global dedup (remove all duplicates) and run-length dedup (only remove consecutive repeats).

---

### Break down the requirements

#### Step 1: Track the last added element

Compare each element to the last one added to the result. Only append if they differ.

#### Step 2: Handle the first element

The first element is always included since there is no previous element to compare against.

---

### The solution

**Single-pass consecutive comparison**

```python
def remove_consecutive_dupes(items: list) -> list:
    if not items:
        return []
    result = [items[0]]
    for i in range(1, len(items)):
        if items[i] != result[-1]:
            result.append(items[i])
    return result
```

> **Time and Space Complexity**
>
> **Time:** O(n) for a single pass through the list.
> 
> **Space:** O(n) for the result list in the worst case where no consecutive duplicates exist.

> **Interviewers Watch For**
>
> Comparing against `result[-1]` instead of `items[i-1]` is slightly cleaner, but both are correct. The key is that only consecutive duplicates are removed.

> **Common Pitfall**
>
> Using a set to track seen elements removes ALL duplicates, not just consecutive ones. `[1, 2, 1]` should stay as-is since the two 1s are not consecutive.

---

## Common follow-up questions

- What if you needed to remove elements that repeat more than k times consecutively? _(Tests extending the comparison to track run length.)_
- How would you do this in-place without extra space? _(Tests using a write pointer to overwrite elements in the original list.)_
- What is the SQL equivalent of this operation? _(Tests knowledge of LAG/LEAD window functions for consecutive comparison.)_

## Related

- [All practice problems](https://datadriven.io/problems)
- [Mock interview mode](https://datadriven.io/interview/the_streak_breaker)
- [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.