The Repeat Offenders

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
L6
Asked in
Natera

Problem

Given a list, return the values that appear more than once, each listed only once, in the order of their first appearance in the input.

Repetition is a clue.

Example 1

Input:

items = [1,2,3,2,1,4]

Output:

[1,2]

Example 2

Input:

items = [5,5,5]

Output:

[5]

Worked solution and explanation

Why this problem exists in real interviews

This tests duplicate detection with order preservation. It checks whether candidates can use a dictionary to track counts while maintaining the order of first appearance for the output.


Break down the requirements

Step 1: Count occurrences of each item

Build a frequency map using a dictionary.

Step 2: Collect items with count greater than 1

Iterate through the original list (not the dict) to preserve first-appearance order. Add each item to the result the first time its count exceeds 1.


The solution

Frequency count then order-preserving extraction
def find_duplicates(items):
    counts = {}
    for item in items:
        if item in counts:
            counts[item] += 1
        else:
            counts[item] = 1
    seen = set()
    result = []
    for item in items:
        if counts[item] > 1 and item not in seen:
            result.append(item)
            seen.add(item)
    return result

Common follow-up questions