Foundations

Counting Operations

Difficulty: Beginner

In Algorithms and Efficiency you saw the core question: when two programs solve the same problem, how do you know which one scales better? The answer lies in counting. Before any formal notation, before any mathematical symbols, there is...

Learn
/
Foundations
/

Counting Operations

0%
BEGINNER
Complexity varies

Counting Operations

In Algorithms and Efficiency you saw the core question: when two programs solve the same problem, how do you know which one scales better? The answer lies in counting. Before any formal notation, before any mathematical symbols, there is a hands-on skill that every strong programmer develops: looking at a piece of code and tallying the work it performs as the input grows.

Counting Operations teaches exactly that skill. You will learn what counts as a single operation, how to trace a loop and tally its iterations, how to build an operations table that reveals the relationship between input size and work, and what happens when loops are nested. By the end of this lesson you will be able to look at a code snippet and say: "for input size n = 10 this runs about 10 steps; for n = 100, about 100 steps; for n = 1000, about 1000 steps" and mean it precisely.

You will also compare two solutions to the same problem side by side -- counting operations for each as n grows -- and experience the lightbulb moment: when inputs are small both solutions feel fine, but as n climbs the difference becomes impossible to ignore. That concrete, tangible understanding of scaling behaviour is the foundation everything else rests on.

In the next lesson, Asymptotic Analysis Fundamentals, you will take exactly these operation counts and formalize them with the mathematical framework that makes comparison rigorous. Everything you practice counting here becomes the raw material for the O() notation and growth-rate rankings you will learn there.

Beginner
35 min
5 Objectives
5 Sections

Topics:

Foundations
Beginner
Free
Loops & Iteration
Efficiency
Fundamentals

What You'll Learn

By the end of this lesson, you will be able to:

Identify what constitutes a basic operation in code.

Count the total operations in a short code snippet for a given input size n.

Build an operations table showing how work grows as n increases.

Explain why nested loops produce n-squared operations.

Compare two solutions by their operation counts and identify which scales better.

Why This Matters

01

Counting operations is the concrete skill that makes asymptotic analysis meaningful rather than abstract symbol manipulation.

02

Building operations tables by hand gives you the intuition to instantly recognize linear, quadratic, and other growth patterns in real code.

03

This skill is tested directly in interviews: you are expected to count and compare work across different approaches to the same problem.

Key Terms

7 terms you'll encounter in this lesson

1

Operation

A single, indivisible computational step that takes roughly constant time regardless of input size. Examples: assigning a variable, comparing two values, adding two numbers, reading an array element by index.

2

Operation count

The total number of basic operations an algorithm performs for an input of size n. Expressed as a formula in terms of n.

3

Operations table

A table mapping specific values of n to the corresponding operation count. Reveals the growth pattern (linear, quadratic, etc.) at a glance.

4

Input size (n)

The measure of how much data an algorithm must process -- typically the length of an array, string, or number of nodes.

5

Linear growth

When the operation count grows proportionally to n. Doubling n doubles the work. Example: a single loop from 0 to n.

6

Quadratic growth

When the operation count grows as n times n (n squared). Doubling n quadruples the work. Example: two nested loops each running n times.

7

Nested loop

A loop placed inside another loop. If both loops run n times, the inner body executes n times n = n squared times total.

Related Problems

Coding problems that put this lesson's concepts into practice

Heads Up

Common misconceptions to watch out for

"Every line of code is one operation"

A single line can contain multiple operations. For example, `total += arr[i]` performs an array read, an addition, and an assignment -- three operations. For counting growth rates, the exact number per line matters less than understanding that each line is a fixed (constant) amount of work.

"Nested loops always mean n squared"

Only if both loops run up to n. If the outer loop runs n times but the inner runs a fixed number of times (say, 3), the total is 3n -- linear, not quadratic. Always ask: what does each loop variable depend on?

"The difference only matters for huge inputs"

For n = 1000, a linear algorithm might do 1,000 operations while a quadratic one does 1,000,000. That is a 1000x difference -- noticeable even on fast hardware. The gap appears sooner than most people expect.