The Shadow Cleaner
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
- Asked in
- Natera
Problem
Return a new list of items with every repeat after a value's first appearance dropped, the way we clean the ID columns in our event exports. Write the cleanup yourself: a one-call helper that collapses the whole list at once, such as list(set(items)) or dict.fromkeys(items), is off limits, though keeping your own record of values already seen is fine.
Remove the repeats. No shortcuts.
Example
Input:
items = [3,1,4,1,5,9,2,6,5,3]
Output:
[3,1,4,5,9,2,6]
Worked solution and explanation
Why this problem exists in real interviews
This tests order-preserving deduplication without built-in shortcuts. It probes whether a candidate understands how to track seen elements manually using a set for O(1) lookups while preserving insertion order with a list.
Break down the requirements
Step 1: Track which elements have been seen
Use a set (built incrementally, not via set() constructor on the full list) to record elements as you encounter them.
Step 2: Iterate and collect first occurrences
For each element, if it has not been seen, add it to both the result list and the seen set.
The solution
def dedup(items: list) -> list:
seen = set()
result = []
for item in items:
if item not in seen:
seen.add(item)
result.append(item)
return resultCommon follow-up questions
- What if elements are unhashable, like dicts? (Tests whether you would fall back to a list-based seen check or serialize elements.)
- How would you dedup by a specific key in a list of dicts? (Tests extracting a hashable key and tracking it separately.)
- What if you needed to keep the last occurrence instead of the first? (Tests reversing the input, deduping, then reversing back.)