L2 · Working engineerDictionaries & hashing~4 min · 4 tests#34

Find Duplicate Values in a Dictionary

List every value that appears under more than one key in a Python dictionary, in first-seen order, with a counting pass in O(n). Live tests included.

The problem

Return the values that appear under more than one key, each once, in the order they are first seen.

Examples

  1. Example 1

    Input

    duplicate_values({'a': 10, 'b': 20, 'c': 10, 'd': 30, 'e': 20})

    Expected output

    [10, 20]
  2. Example 2

    Input

    duplicate_values({'a': 1, 'b': 2})

    Expected output

    []

+ 2 hidden tests on Submit.

Edge cases to ask about

  • No duplicates
  • All the same value
  • Empty dict

Hints

0/3

    How an interviewer scores this

    0/9
    Python 3.13 · duplicate_values
    ⌘/Ctrl + Enter runs the examples

    Your code runs in real CPython inside your browser — nothing is sent anywhere. The first run downloads the interpreter (about 6 MB, once). Your code is saved on this device as you type.

    Complexity Lab

    What does this cost as n grows?

    Interviewers score the analysis as much as the code. Commit to an answer first — then check it, and measure your code against the optimal one at growing input sizes.

    Time complexity of the optimal solution
    Space complexity (extra memory)

    Pick both to reveal the answer.

    Measure it

    Runs the function on inputs of size 250 up to 16,000 and records the time and peak memory. Slow solutions stop early — a short curve is itself the answer.

    From brute force to optimal

    The progression an interviewer wants to hear, one step at a time.

    ApproachTimeSpaceIdea
    list(values).count per valueO(n²)O(n)Recounts each time.
    bestCount values onceO(n)O(n)Dict insertion order gives first-seen order for free.
    Walkthrough of the optimal approach (try it yourself first)

    Count the values, keep those with count > 1. Because dicts keep insertion order, the result is already in first-seen order.

    Complexity: O(n) time, O(n) space. One pass to count values and one over the distinct values.

    Reveal the reference solution
    def duplicate_values(d):
        counts = {}
        for v in d.values():
            counts[v] = counts.get(v, 0) + 1
        return [v for v, c in counts.items() if c > 1]

    Follow-ups interviewers ask

    • Return {value: [keys]} for the duplicates.
    • Values are unhashable lists.

    Frequently asked interview questions

    Core interview concepts, complexities, and follow-ups scored by hiring teams.

    What is the time complexity of Find Duplicate Values in a Dictionary in Python?

    The optimal solution runs in O(n) time and O(n) auxiliary space. One pass to count values and one over the distinct values.

    What is the brute-force approach, and how do you optimise it?

    list(values).count per value: O(n²) time, O(n) space. Recounts each time. Count values once: O(n) time, O(n) space. Dict insertion order gives first-seen order for free.

    What follow-up questions do interviewers ask about Find Duplicate Values in a Dictionary?

    Return {value: [keys]} for the duplicates. Values are unhashable lists.