DSA series #01 · interactive

One problem, three ways

DSA is not about writing code. It is about spotting the pattern and knowing more than one way to solve it. Here is the classic example: find two numbers that add up to 24.

Try each number with every number after it.

4
[0]
↑i
11
[1]
↑j
7
[2]
1
[3]
15
[4]
9
[5]
3
[6]
12
[7]

4 + 11 = 15 ≠ 24

1

checks

Trade-off: No extra memory, easy to get right. Falls over once n grows. time O(n²) · extra memory O(1)

Checks to find the answer in these 8 numbers

With 8 numbers, checking every pair is not slow, and sorting first actually costs more. Big-O ignores constants and small inputs. That is why you measure before you optimise.

Now make the input bigger

log scale · 1 check = 1 ns

Same problem, same answer. At a million numbers the pair check takes minutes, the hash map about a millisecond. Knowing the pattern is what lets you pick.