What Input Size Rules Out

Concepts covered: Sets

A problem statement often says how big the input can get: up to 1,000 rows, up to 100,000 events, an answer within 1 second. Those limits are design information. Before writing code, you can estimate how many steps each candidate approach takes and discard the ones that cannot finish in time.

Count steps, then compare with a budget

A loop over n items runs its body n times, and 2 passes run 2 × n bodies. A loop inside a loop that compares every item with every later item runs about n × n ÷ 2 bodies, because the first item is compared with n - 1 others, the second with n - 2, and so on. For a working budget, take around 10 million simple steps per second in Python: a conservative figure for loop bodies doing a comparison or an addition, which you can confirm by timing a loop on your own machine.

The animation runs 2 approaches to a duplicate check on 6 IDs and counts the steps each one takes.

ids = [7, 3, 9, 3, 5, 8]

pair_checks = 0
dup_nested = False
for i in range(len(ids)):
    for j in range(i + 1, len(ids)):
        pair_checks += 1
        if ids[i] == ids[j]:
            dup_nested = True

dict_steps = 0
counts = {}
for x in ids:
    dict_steps += 1
    counts[x] = counts.get(x, 0) + 1

print(pair_checks, dup_nested)
print(dict_steps, counts)
15 True
6 {7: 1, 3: 2, 9: 1, 5: 1, 8: 1}

At 6 IDs the gap is 15 against 6. The table pushes n to sizes a pipeline actually sees and converts steps to time with the budget of 10 million simple steps per second.

n (items)Steps, 1 passTime, 1 passPair checksTime, pair checks
1,0001,0000.0001 s499,5000.05 s
100,000100,0000.01 sabout 5 billionabout 8 minutes
10,000,00010,000,0001 sabout 50 trillionabout 58 days

Read the table against a limit of 100,000 events and 1 second. Pair checks need roughly 8 minutes, so any solution shaped like a loop inside a loop is ruled out before it is written, however clean it looks. One pass or 2 passes cost about 0.01 to 0.02 seconds and fit with room to spare. At 10 million rows a single pass costs about 1 second, so a 1-second limit allows only one lean pass; a 5-second limit allows a handful of passes, and still nothing nested.

Small limits allow the direct approach

The arithmetic also works in the other direction. If a task guarantees at most 1,000 rows, the pair loop finishes in about 0.05 seconds, and its directness becomes an advantage: it reads like the problem statement and leaves little room for mistakes. A stated limit tells you which approaches are forbidden, and also which ones you are allowed to keep plain.

Loops hidden inside a single line

Some loops do not look like loops. x in some_list, some_list.count(x), some_list.index(x) and some_list.remove(x) each scan the list from the front, so calling one inside a for loop builds a loop inside a loop. The dedup below has a single visible for, but user not in seen walks seen on every row.

events = ["ana", "bo", "ana", "cy", "bo", "dee"]

seen = []
for user in events:
    if user not in seen:
        seen.append(user)

print(seen)

With 1 million mostly distinct users, seen grows toward 1 million names, and each later row scans most of it: hundreds of billions of comparisons. A dict behaves differently. key in counts and counts[key] take about the same time whether the dict holds 10 keys or 10 million, which is why the counting pass in the animation stays at n steps.

A task gives up to 200,000 transactions and expects an answer in about 1 second. With a budget of 10 million simple steps per second, which approach is ruled out before coding?

The pair count has an exact form. The first item meets n - 1 later items, the second n - 2, down to 0 for the last, and those add up to n * (n - 1) // 2. For the 6 IDs in the animation that is 6 × 5 ÷ 2 = 15, the pair_checks it printed; for 1,000 items it is 499,500, the figure in the table. Turning a limit into a verdict is then one comparison: compute the steps for the largest allowed n and check whether they stay within the budget, 10 million steps for a 1-second limit.

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.