Comparing 2 Working Solutions

Concepts covered: Collections

Solutions that both pass every test can still differ in what they cost. One may finish in a fraction of a second where the other takes minutes, one may hold an extra dict the size of the input, and one may read like the problem statement while the other needs explaining. Choosing between them is a judgment made against the input limits and the people who will maintain the code.

Finding the first one-time user with count() and with a dict

def first_once_count(events):
    for user in events:
        if events.count(user) == 1:
            return user
    return None

def first_once_dict(events):
    counts = {}
    for user in events:
        counts[user] = counts.get(user, 0) + 1
    for user in events:
        if counts[user] == 1:
            return user
    return None

events = ["ana", "bo", "ana", "cy", "bo", "dee"]
print(first_once_count(events))
print(first_once_dict(events))

Output:

cy
cy

Both print cy. first_once_count is 4 lines and says what it means: the first user whose count in the log is 1. Its cost hides in events.count(user), which reads the whole list on every call. first_once_dict takes 2 loops and a dict, and reads the log exactly twice.

Speed, memory and clarity side by side

Measurefirst_once_countfirst_once_dict
Reads of a log with no one-time usern × n2 × n
n = 1,0001,000,000 reads2,000 reads
n = 100,00010 billion reads, about 17 minutes200,000 reads, about 0.02 s
Extra memoryNothing beyond the listOne dict entry per distinct user
ClarityMirrors the task in a single conditionTwo loops; the reader must see why

The table assumes a log with no one-time user, the input that makes both functions read to the end. There the count version calls count n times, each a full scan, while the dict version finishes its 2 passes and returns None. The times use the same budget as before, 10 million simple steps per second.

A rule for choosing

Start from the stated limits. Discard any solution whose step count does not fit the budget; that decision needs no taste. Among the ones that fit, prefer the one a teammate can verify fastest, which usually means the shortest code that states the task directly. Memory decides only when the extra structure would not fit, for example a dict over hundreds of millions of distinct keys on a single machine.

Keep first_once_count when
  • The log is capped at roughly 1,000 events
  • The function runs once in a script, not once per row of a pipeline
  • Readers need to check it against the spec at a glance
Switch to first_once_dict when
  • The log can reach 100,000 events or more
  • The function runs inside a loop over many logs
  • The next request, every one-time user, needs the counts anyway

The last row points at a fourth measure: how each solution absorbs the next change. Once the counts exist, listing every one-time user, or every user seen more than 5 times, is an edit to the second loop's condition. The count version would repeat its full scans for each new question.

A nightly job checks a config list capped at 50 entries for any name that appears twice. One correct version calls names.count(name) > 1 inside a loop; another builds a dict of counts first. Which should the team keep?

The count version has a second flaw once the task changes from 'is there a duplicate' to 'list the duplicates'. A loop over ids that prints every ID passing ids.count(i) > 1 prints it once per appearance, so an ID that occurs 3 times prints 3 times. Walking the dict of counts visits each key exactly once, in the order it was first inserted, which is the order of its first appearance in the list. Counting in pass 1 and filtering the dict in pass 2 gives one line per duplicated ID.

About this lesson

This section is part of the Problem Solving: Intermediate lesson on DataDriven, the home of data engineering. Lessons cover SQL, Python, Spark, data modeling and pipeline architecture, and each section pairs its explanation with worked examples and practice you run as you go.