The Type Sorter

An easy Python interview practice problem on DataDriven. Write the Python and run it against test cases, with instant feedback.

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.

A mixed list is hiding its numbers - extract them.

Example

Input:

items = [1,"hello",2,null,3.5,4,true]

Output:

[1,2,4]

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
def filter_ints(items: list) -> list:
    result = []
    for item in items:
        if type(item) is int:
            result.append(item)
    return result

Common follow-up questions