# The Odd Filter

> Strip out everything that does not belong to the odd club.

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

Domain: Python · Difficulty: easy · Seniority: L3

## Problem

Given a string of digit characters, return a string containing only the odd digits in the original order.

## Worked solution and explanation

### Why this problem exists in real interviews

This tests **basic string traversal and character classification**. It checks whether candidates can iterate through characters, check conditions, and build a result string, all fundamental operations in text processing.

---

### Break down the requirements

#### Step 1: Iterate through each character

Scan the input string character by character.

#### Step 2: Check if the digit is odd

Convert the character to an integer and check if it is odd using modulo.

#### Step 3: Build the result

Append matching digits and join them into the output string.

---

### The solution

**Character-by-character digit filtering**

```python
def filter_odd_digits(s):
    result = []
    for ch in s:
        if int(ch) % 2 != 0:
            result.append(ch)
    return ''.join(result)
```

> **Time and Space Complexity**
>
> **Time:** O(n) where n is the string length.
> 
> **Space:** O(k) where k is the number of odd digits.

> **Interviewers Watch For**
>
> Do you handle the empty string edge case? An empty string produces an empty result naturally since the loop does not execute.

> **Common Pitfall**
>
> Using string concatenation (`result += ch`) inside the loop. In Python, this creates a new string each time, leading to O(n^2) behavior. Using a list and `join` is O(n).

---

## Common follow-up questions

- What if the input contained non-digit characters? _(Tests adding an `isdigit()` guard before the int conversion.)_
- How would you return the even digits instead? _(Tests flipping the condition.)_
- What if you needed both odd and even digits as separate results? _(Tests building two result lists in a single pass.)_

## Related

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