# The Odd Extractor

> Not all numbers from a string are welcome here.

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

Domain: Python · Difficulty: easy · Seniority: L3

## Problem

Given a string of whitespace-separated tokens, parse each as int. Return a list containing only the odd integers in original order.

## Worked solution and explanation

### Why this problem exists in real interviews

This tests **string splitting, type conversion, and filtering** in sequence. It is a common ETL pattern where raw text data needs to be parsed, converted to the appropriate type, and filtered before downstream use.

---

### Break down the requirements

#### Step 1: Split the string on spaces

Produce a list of string tokens.

#### Step 2: Convert each token to an integer

Use `int()` to parse each string token.

#### Step 3: Filter for odd values

Keep only integers where `n % 2 != 0`.

---

### The solution

**Split, convert, filter in sequence**

```python
def extract_odds(s):
    tokens = s.split()
    result = []
    for token in tokens:
        num = int(token)
        if num % 2 != 0:
            result.append(num)
    return result
```

> **Time and Space Complexity**
>
> **Time:** O(n) where n is the number of tokens.
> 
> **Space:** O(k) where k is the number of odd values.

> **Interviewers Watch For**
>
> Do you handle the empty string case? `''.split()` returns an empty list, so the loop naturally produces an empty result. No special case needed.

> **Common Pitfall**
>
> Returning strings instead of integers. The problem asks for a list of ints, so forgetting the `int()` conversion produces the wrong output type.

---

## Common follow-up questions

- What if some tokens are not valid integers? _(Tests wrapping int() in try/except for graceful handling.)_
- What if negative numbers should also be included? _(Tests that `int('-3')` works and -3 is odd.)_
- How would you process a very long file line by line? _(Tests generator-based or streaming approaches.)_

## Related

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