# The Odd Digits

> Hidden inside a mess of characters are a few odd numbers.

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

Domain: Python · Difficulty: easy · Seniority: L3

## Problem

Given a string, extract only the characters that are digits and whose integer value is odd. Preserve input order. Return the resulting string.

## Worked solution and explanation

### Why this problem exists in real interviews

This tests **character filtering with dual conditions**: checking if a character is a digit and whether its numeric value is odd. It reveals whether candidates can chain conditions cleanly in a single pass.

---

### Break down the requirements

#### Step 1: Iterate through each character

Scan the string one character at a time.

#### Step 2: Check two conditions: is digit and is odd

Use `ch.isdigit()` to confirm it is numeric, then `int(ch) % 2 != 0` to check oddness.

#### Step 3: Build the result string

Collect matching characters and join them.

---

### The solution

**Single-pass digit extraction with oddness check**

```python
def odd_digits(s):
    result = []
    for ch in s:
        if ch.isdigit() and 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 found.

> **Interviewers Watch For**
>
> Do you check `isdigit()` before `int(ch)`? Calling `int()` on a non-digit character raises a ValueError. The order of conditions matters.

> **Common Pitfall**
>
> Forgetting that '0' is an even digit. Some candidates include '0' in the output by confusing 'odd digit' with 'digit character'.

---

## Common follow-up questions

- What if you needed even digits instead? _(Tests flipping the condition to `int(ch) % 2 == 0`.)_
- What if the string could contain Unicode digit characters? _(Tests awareness that `isdigit()` matches more than ASCII 0-9.)_
- How would you return the positions of odd digits instead of the characters? _(Tests tracking index alongside character filtering.)_

## Related

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