# Find Indices

> It is in there somewhere. Where exactly?

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

Domain: Python · Difficulty: medium · Seniority: L3

## Problem

Given a list and a target, return a list of the ascending-sorted indices where list[i] == target.

## Worked solution and explanation

### Why this problem exists in real interviews

Finding all positions of a target value tests basic **indexed iteration** and result accumulation. It checks whether you use `enumerate` or manual indexing and whether your output is correctly ordered.

---

### Break down the requirements

#### Step 1: Iterate with index tracking

Use `range(len(lst))` or `enumerate` to access both the index and the value.

#### Step 2: Collect indices where the value matches

Compare each element to the target and append the index if they match.

---

### The solution

**Index-tracked scan with equality check**

```python
def index_of_all(lst, target):
    result = []
    for i in range(len(lst)):
        if lst[i] == target:
            result.append(i)
    return result
```

> **Time and Space Complexity**
>
> **Time:** O(n) where n is the list length. Every element is checked once.
> 
> **Space:** O(k) where k is the number of matching indices.

> **Interviewers Watch For**
>
> Using `==` for comparison rather than `is`. The `is` operator checks identity, not equality, and would fail for equal but non-identical objects.

> **Common Pitfall**
>
> Using `list.index()` in a loop. This re-scans from the beginning each time, making the approach O(n^2) for repeated values.

---

## Common follow-up questions

- What if the list is sorted and you need the range of matching indices? _(Tests binary search for first and last occurrence using bisect.)_
- How would you return the indices of all elements matching a predicate? _(Tests generalizing from equality to a callable filter function.)_
- What if the list is very large and you want lazy evaluation? _(Tests generator-based approach that yields indices one at a time.)_

## Related

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