# The Carousel

> Keep moving, same ride.

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

Domain: Python · Difficulty: easy · Seniority: L3

## Problem

Given a list and integer k (possibly >= len(lst) or negative), return the list rotated left by k positions. Rotating left by k moves index i to index (i - k) mod n.

## Worked solution and explanation

### Why this problem exists in real interviews

Left-rotating a list by k positions tests **modular arithmetic** and **slice manipulation**. It is the mirror of right rotation and checks the same index normalization skills.

---

### Break down the requirements

#### Step 1: Normalize k with modulo

Use `k % len(lst)` to handle k larger than the list length.

#### Step 2: Split and recombine

Take the first k elements and move them to the end.

---

### The solution

**Modulo-normalized left rotation with slicing**

```python
def rotate_left(lst, k):
    if not lst:
        return []
    k = k % len(lst)
    result = lst[k:] + lst[:k]
    return result
```

> **Time and Space Complexity**
>
> **Time:** O(n) for slicing and concatenation.
> 
> **Space:** O(n) for the new list.

> **Interviewers Watch For**
>
> The `k % len(lst)` normalization. Without it, k larger than the list length produces incorrect results.

> **Common Pitfall**
>
> Not handling empty lists. `len([])` is 0, and `k % 0` raises a ZeroDivisionError.

---

## Common follow-up questions

- How would you rotate in place with O(1) extra space? _(Tests the three-reverse algorithm: reverse first k, reverse the rest, reverse the whole list.)_
- What is the relationship between left and right rotation? _(Tests that left rotation by k equals right rotation by `n - k`.)_
- What if the list is a deque? _(Tests `collections.deque.rotate(-k)` for O(k) in-place rotation.)_

## Related

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