# The Integer Sieve

> Not everything in this list belongs here.

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

Domain: Python · Difficulty: easy · Seniority: L3

## Problem

Given a mixed list, return a new list of elements whose type is exactly int (not bool). Preserve input order.

## Worked solution and explanation

### Why this problem exists in real interviews

This tests a subtle Python gotcha: **bool is a subclass of int**. `isinstance(True, int)` returns `True`, so naive type checking includes booleans. It probes awareness of Python's type hierarchy and precise filtering logic.

> **Trick to Solving**
>
> Use `type(x) is int` instead of `isinstance(x, int)`. The `is` check with `type()` returns `False` for booleans, while `isinstance` returns `True` because `bool` inherits from `int`.

---

### Break down the requirements

#### Step 1: Filter by exact type

Only include elements where `type(x) is int`. This excludes booleans.

#### Step 2: Build and return the filtered list

Iterate through the input and collect qualifying elements.

---

### The solution

**Exact type check to exclude booleans**

```python
def filter_ints(items: list) -> list:
    result = []
    for item in items:
        if type(item) is int:
            result.append(item)
    return result
```

> **Time and Space Complexity**
>
> **Time:** O(n) single pass.
> 
> **Space:** O(k) where k is the number of integers in the input.

> **Interviewers Watch For**
>
> Whether you know the `bool` subclass trap. Candidates who use `isinstance(x, int)` will incorrectly include `True` and `False`.

> **Common Pitfall**
>
> Using `isinstance(x, int)` which includes booleans. `isinstance(True, int)` is `True` in Python because `bool` is a subclass of `int`.

---

## Common follow-up questions

- Why is bool a subclass of int in Python? _(Tests knowledge of Python's history: booleans were added later and inherit from int for backward compatibility.)_
- What does `True + True` evaluate to in Python? _(Tests that it evaluates to 2, since `True` is int 1.)_
- How would you also filter out numpy integer types? _(Tests checking against multiple types or using a type whitelist.)_

## Related

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