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