The Odd Extractor
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 string of whitespace-separated tokens, parse each as int. Return a list containing only the odd integers in original order.
Not all numbers from a string are welcome here.
Example 1
Input:
s = "1 2 3 4 5 6 7"
Output:
[1,3,5,7]
Example 2
Input:
s = "10 15 22 33"
Output:
[15,33]
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
def extract_odds(s):
tokens = s.split()
result = []
for token in tokens:
num = int(token)
if num % 2 != 0:
result.append(num)
return resultCommon 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.)