# The Type Sorter

> A mixed list is hiding its numbers - extract them.

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

Domain: Python · Difficulty: easy · Seniority: L3

## Problem

Given a mixed list, return a new list containing only values whose type is exactly int (not bool, not float). Preserve order.

## Worked solution and explanation

### Why this problem exists in real interviews

This tests **type checking** in Python, specifically the subtlety that `bool` is a subclass of `int`. It probes whether a candidate understands Python's type hierarchy and can filter precisely.

---

### Break down the requirements

#### Step 1: Iterate through the mixed list

Check each element's type to determine if it qualifies as a pure integer.

#### Step 2: Exclude booleans explicitly

Since `isinstance(True, int)` returns `True` in Python, you must check `type(x) is int` or add an explicit boolean exclusion.

---

### The solution

**Strict type check excluding bool subclass**

```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) for a single pass through the list.
> 
> **Space:** O(n) for the result list in the worst case.

> **Interviewers Watch For**
>
> Using `type(x) is int` instead of `isinstance(x, int)`. The `isinstance` check returns True for booleans, which violates the requirement to exclude them.

> **Common Pitfall**
>
> `isinstance(True, int)` is `True` because `bool` is a subclass of `int` in Python. This is the single most tested edge case for this problem.

---

## Common follow-up questions

- When should you use isinstance vs type() is? _(Tests understanding of polymorphism: isinstance is preferred for duck typing, type() for strict checking.)_
- What if you needed to filter for numeric types including float? _(Tests `isinstance(x, (int, float)) and not isinstance(x, bool)`.)_
- How would you handle numpy integer types? _(Tests awareness of `numpy.integer` as a separate type hierarchy.)_

## Related

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