Unique Values
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
- Tata Consultancy Services
Problem
An event stream keeps emitting the same values more than once. Return a new list that keeps only the first time each value appears, in the order they first showed up.
Duplicates are noise. Remove them.
Example 1
Input:
items = [3,1,4,1,3,2]
Output:
[3,1,4,2]
Example 2
Input:
items = [7,7,7]
Output:
[7]
Worked solution and explanation
What this is really testing
This is order-preserving deduplication wearing a data-cleaning costume. The skill being probed: can you keep the first occurrence of each value and drop the rest without disturbing the order they arrived in? Almost everyone knows how to dedupe. The trick is doing it while holding onto order, because the obvious one-liner throws order away.
The trap is that list(set(items)) looks like the answer. It dedupes perfectly, and on these samples it even comes back looking ordered by luck. Feed it an input whose first-occurrence order is not sorted, and the result scrambles.
Why one tool is not enough
Step 1: You need the set for the lookup
A set gives you O(1) membership so you can ask 'have I seen this?' in constant time. But a set has no order.
Step 2: You need the list for the order
A list preserves the order things were appended. So you drive both together: consult the set, and append to the list only on the first sighting of a value.
The solution
def remove_dupes(items: list) -> list:
seen = set()
result = []
for item in items:
if item not in seen:
seen.add(item)
result.append(item)
return resultOne pass: the set answers membership, the list carries order.
- list(set(items))
- Dedupes, but a set is unordered, so the result order is whatever the hash layout gives you. On these samples it happens to look ascending, which is exactly why the bug survives review.
- seen-set + list
- Dedupes and preserves first-occurrence order, because you append in the order values first appear. Correct on any input, not just the lucky ones.
Common follow-up questions
- What if the elements were unhashable, like dicts? (Tests falling back to a list for seen-tracking (O(n^2)) or serializing elements to a hashable key.)
- How would you deduplicate by a key function instead of by the value itself? (Tests using key(item) for the seen set while collecting the original items.)
- What is the SQL equivalent, and does it preserve order? (Tests SELECT DISTINCT versus GROUP BY and how ordering is or is not guaranteed.)