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

Find All Keys With the Maximum Value

Return every key whose value equals the dictionary's maximum, in insertion order, in O(n). Handles ties that max(d, key=d.get) misses.

The problem

Return a list of all keys whose value equals the maximum value in d, in insertion order. Return [] for an empty dict.

Examples

  1. Example 1

    Input

    keys_with_max({'a': 10, 'b': 25, 'c': 25, 'd': 15})

    Expected output

    ['b', 'c']
  2. Example 2

    Input

    keys_with_max({'x': 1})

    Expected output

    ['x']

+ 2 hidden tests on Submit — negatives.

Edge cases to ask about

  • Empty dict
  • Ties
  • Negative values

Hints

0/3

    How an interviewer scores this

    0/9
    Python 3.13 · keys_with_max
    ⌘/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
    max(d, key=d.get)O(n)O(1)Returns only ONE key — misses ties.
    bestFind the max, then filterO(n)O(k)Two passes, every tied key.
    Walkthrough of the optimal approach (try it yourself first)

    Compute top = max(d.values()), then a comprehension keeps every key equal to it. max(d, key=d.get) is the trap — it returns a single key, so ties are lost.

    Complexity: O(n) time, O(k) space. One pass for max(), one to filter; the result holds the k tied keys.

    Reveal the reference solution
    def keys_with_max(d):
        if not d:
            return []
        top = max(d.values())
        return [k for k, v in d.items() if v == top]

    Follow-ups interviewers ask

    • Do it in a single pass.
    • Return the keys with the k largest values.

    Frequently asked interview questions

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

    What is the time complexity of Find All Keys With the Maximum Value in Python?

    The optimal solution runs in O(n) time and O(k) auxiliary space. One pass for max(), one to filter; the result holds the k tied keys.

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

    max(d, key=d.get): O(n) time, O(1) space. Returns only ONE key — misses ties. Find the max, then filter: O(n) time, O(k) space. Two passes, every tied key.

    What follow-up questions do interviewers ask about Find All Keys With the Maximum Value?

    Do it in a single pass. Return the keys with the k largest values.