The Solo Act
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 list of integers, return a list of the values that appear exactly once, preserving input order.
One-and-done values only.
Example 1
Input:
codes = [200,404,200,500]
Output:
[404,500]
Example 2
Input:
codes = [1,2,3,2,1]
Output:
[3]
Worked solution and explanation
Why this problem exists in real interviews
This tests frequency counting followed by order-preserving filtering. It is a two-pass pattern: first count occurrences, then filter to elements that appeared exactly once while maintaining original order.
Break down the requirements
Step 1: Count occurrences of each code
Build a frequency dict in one pass over the list.
Step 2: Filter to codes with count exactly 1, preserving order
Iterate the original list a second time, collecting codes whose count is 1.
The solution
def find_singletons(codes: list) -> list:
counts = {}
for code in codes:
counts[code] = counts.get(code, 0) + 1
result = []
for code in codes:
if counts[code] == 1:
result.append(code)
return resultCommon follow-up questions
- What if you needed singletons in sorted order instead of original order? (Tests adding a sort step after filtering.)
- How would you find the first singleton only? (Tests short-circuiting the second pass after the first match.)
- What if the input stream was too large to fit in memory? (Tests external sorting or streaming frequency estimation techniques.)