The Original Keeper
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
- Amazon
Problem
Given a list of items (possibly with duplicates), return a new list containing only the first occurrence of each value, preserving input order.
Clean up duplicate events without losing the timeline.
Example
Input:
items = [1,1,3,2,5,6,5]
Output:
[1,3,2,5,6]
Worked solution and explanation
Why this problem exists in real interviews
This tests order-preserving deduplication, a fundamental ETL operation. It checks whether candidates use a set for O(1) lookups while maintaining the original insertion order in the output.
Break down the requirements
Step 1: Track seen values in a set
Use a set for O(1) membership checks.
Step 2: Iterate in order, keeping first occurrences
For each element, if it has not been seen, add it to both the result list and the seen set.
Step 3: Skip duplicates
If the value is already in the seen set, skip it.
The solution
def deduplicate(items):
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 (e.g., lists)? (Tests converting to tuples or using a different tracking strategy.)
- What if you needed to count how many times each value was dropped? (Tests adding a counter alongside the seen set.)
- How would you handle this for a very large streaming dataset? (Tests Bloom filters for approximate deduplication at scale.)