Algorithm Efficiency Examples in CS Thinking

Start with the recap, study the fully worked examples, then use the practice problems to check your understanding of Algorithm Efficiency.

This page combines explanation, solved examples, and follow-up practice so you can move from recognition to confident problem-solving in CS Thinking.

Concept Recap

Big-O notation describes the asymptotic growth of an algorithm's cost as input size increases — most often applied to worst-case running time or memory, expressed as O(n)O(n), O(n2)O(n^2), O(logn)O(\log n), etc.

Does doubling the data double the time? Or quadruple it? Or barely change it?

Read the full concept explanation →

How to Use These Examples

  • Read the first worked example with the solution open so the structure is clear.
  • Try the practice problems before revealing each solution.
  • Use the related concepts and background knowledge badges if you feel stuck.

What to Focus On

Core idea: Algorithm efficiency matters increasingly as data grows—a slow algorithm on small data may fail completely on large data.

Common stuck point: Big O ignores constant factors—2n2n and 100n100n are both O(n)O(n) because the growth rate is what matters.

Sense of Study hint: When analyzing efficiency, first identify the input size nn. Then count how many times the most-repeated operation executes as a function of nn (look for nested loops). Finally, express the growth rate using Big O, dropping constants and lower-order terms.

Worked Examples

Example 1

hard
Algorithm A takes 100 steps for an input of size 10 and 400 steps for size 20. Algorithm B takes 10 steps for size 10 and 20 steps for size 20. Which is more efficient and what might their time complexities be?

Answer

Algorithm B is more efficient: O(n)O(n) vs O(n2)O(n^2).

First step

1
Step 1: Algorithm A: when size doubles (10→20), steps quadruple (100→400). This suggests O(n2)O(n^2).

See the full worked solution + why-it-works coaching

SetupKey insightWhy it worksCommon pitfallConnection

Unlock answer keys One Family plan — every worked solution, all subjects

Example 2

hard
Explain why binary search (O(logn)O(\log n)) is more efficient than linear search (O(n)O(n)) for a sorted list of 1,000,000 elements.

Example 3

medium
A function builds a list of nn items by repeatedly prepending. Each prepend on a linked list is O(1)O(1); on a Python list it is O(n)O(n). What total complexity in each case?

Example 4

medium
A function does 3n+53n + 5 operations. What is the Big-O, and why don't constants matter asymptotically?

Example 5

medium
Two algorithms have time complexities O(n2)O(n^2) and O(100n)O(100n). For which nn does the linear algorithm overtake the quadratic in operation count?

Example 6

hard
Quicksort's average-case is O(nlogn)O(n \log n) but worst-case is O(n2)O(n^2). Name two engineering tweaks that keep the worst case rare in practice.

Example 7

hard
BFS and Dijkstra both visit vertices in graph order. On a graph with VV vertices and EE edges, what are their respective complexities, and why does Dijkstra need the extra log factor?

Example 8

hard
An algorithm has time complexity O(nlogn)O(n \log n) and space O(n)O(n). List one practical concern beyond Big-O that affects real-world efficiency.

Example 9

hard
A naive matrix multiplication is O(n3)O(n^3). State two complexity-class improvements and the historical names.

Example 10

challenge
Knuth: 'Premature optimization is the root of all evil.' Explain the engineering principle behind the quote in the context of O(n)O(n) vs O(logn)O(\log n).

Example 11

hard
A function processes a stream of nn items and must report the median after each insertion. What's the most efficient achievable per-operation complexity, and the data structure that achieves it?

Example 12

medium
An algorithm runs in T(n)=1000nT(n) = 1000n ms and another in U(n)=n2U(n) = n^2 ms. Which is faster when n=10n = 10? When n=10000n = 10000?

Example 13

medium
Merge sort runs in O(nlogn)O(n \log n). Roughly how many operations does it perform on n=1,048,576=220n = 1{,}048{,}576 = 2^{20} items?

Example 14

medium
A function calls a O(n)O(n) helper inside a loop that runs nn times. What is the overall time complexity?

Example 15

medium
For n=30n = 30, compare 2n2^n and n2n^2. Which algorithm class would finish, and which would not?

Example 16

hard
Algorithm A runs in TA(n)=100nT_A(n) = 100n, and algorithm B runs in TB(n)=n2T_B(n) = n^2. For what range of nn is algorithm B actually faster?

Example 17

hard
A recursive function makes two calls on inputs of size n/2n/2 and does O(n)O(n) work outside the calls. What is its time complexity?

Example 18

hard
You scan a list of nn items and for each item do a O(logn)O(\log n) binary search in a separate sorted list. What is the total time complexity?

Example 19

hard
You must check if any two of nn numbers sum to a target TT. A naive double loop is O(n2)O(n^2). Describe a more efficient approach and give its complexity.

Example 20

challenge
Suppose you have an algorithm with time complexity O(nlogn)O(n \log n) that takes 11 second on n=106n = 10^6. Approximately how long will it take on n=109n = 10^9?

Practice Problems

Try these problems on your own first, then open the solution to compare your method.

Example 1

hard
Rank these time complexities from most to least efficient: O(n2)O(n^2), O(1)O(1), O(nlogn)O(n \log n), O(n)O(n), O(2n)O(2^n).

Example 2

hard
Algorithm X checks every pair of students in a class of size `n`. Algorithm Y checks each student once. Match each algorithm to `O(n^2)` or `O(n)`, and explain which scales better for `n = 1000`.

Example 3

easy
A loop runs once per element of an array of size n. What is its Big-O time complexity?

Example 4

easy
Binary search on a sorted array of size n has what time complexity?

Example 5

easy
Two nested loops, each running n times, give what complexity?

Example 6

easy
Accessing `a[5]` in an array by index takes what time, regardless of array size?

Example 7

easy
Does Big-O describe the upper bound or the exact running time?

Example 8

easy
Why does algorithm efficiency matter more as data grows?

Example 9

easy
Which is faster for large n: O(n)O(n) or O(n2)O(n^2)?

Example 10

easy
Linear search through n items has what worst-case complexity?

Example 11

medium
An algorithm does `n + n^2` operations. What is its Big-O?

Example 12

medium
An algorithm does `3n + 5` operations. What is its Big-O?

Example 13

medium
For n = 1,000,000, roughly how many steps does an O(log2n)O(\log_2 n) algorithm take?

Example 14

medium
Doubling the input size doubles the runtime. What complexity class is this?

Example 15

medium
Doubling the input size quadruples the runtime. What complexity class is this?

Example 16

medium
For small inputs, an O(n2)O(n^2) algorithm beats an O(n)O(n) one. What does Big-O ignore that explains this?

Example 17

medium
A function loops over an array (n) and, for each element, does a binary search in another sorted array (log n). Total complexity?

Example 18

challenge
Algorithm A is O(n2)O(n^2) with 1 op per step; B is O(n)O(n) with 100 ops per step. For roughly what n does B become faster?

Example 19

challenge
Why is the naive recursive Fibonacci O(2n)O(2^n) rather than O(n)O(n)?

Example 20

challenge
Counting sort runs in O(n+k)O(n + k) where k is the value range. When does it beat comparison sorts' O(nlogn)O(n \log n)?

Example 21

medium
An algorithm halves the input each step until size 1, doing constant work per step. What is its complexity?

Example 22

medium
An algorithm does `n^2 + 1000n` operations. What is its Big-O for large n?

Example 23

easy
Doubling the input doubles a hash-table lookup time from 1μs1\mu s to 1μs1\mu s (no change). What complexity is this consistent with?

Example 24

easy
An algorithm has nested loops, the outer over nn items and the inner over the same nn items. What is its time complexity?

Example 25

easy
Allocating an array of nn integers requires what space complexity?

Example 26

medium
Merge sort divides into two halves, sorts each, and merges. Solve T(n)=2T(n/2)+O(n)T(n) = 2T(n/2) + O(n) for the complexity.

Example 27

medium
For n=1000n = 1000, compare the rough number of operations: O(n2)O(n^2) vs O(nlogn)O(n \log n).

Example 28

medium
A naive recursive Fibonacci has complexity O(ϕn)O(\phi^n). Why is memoization a huge win, and what's the resulting complexity?

Example 29

medium
Cache-friendly access: why is iterating a 2D array row-by-row often much faster than column-by-column in row-major languages?

Example 30

hard
A program is I/O-bound — most time is spent waiting on disk. Will switching from O(n2)O(n^2) to O(nlogn)O(n \log n) help?

Example 31

hard
Two-sum problem: find indices i,ji, j with `arr[i] + arr[j] = target`. What's the brute-force complexity and what's the best?

Example 32

hard
Time-space tradeoff: a precomputed lookup table can answer queries in O(1)O(1) instead of O(logn)O(\log n) each. When is this worth it?

Example 33

medium
Pseudocode: `for i in 1..n: for j in 1..i: ...`. What is the total inner-body count?

Example 34

medium
Python list `in` membership test is O(n)O(n). What single data-structure swap gives O(1)O(1) membership?

Example 35

challenge
Rank the time complexity classes in order from FASTEST to SLOWEST growth: O(n!)O(n!), O(n3)O(n^3), O(n)O(\sqrt{n}), O(loglogn)O(\log \log n), O(nlogn)O(n^{\log n}).

Example 36

easy
What is the Big-O time complexity of a single loop that prints each element of an array of size nn?

Example 37

easy
Hashing a key into a hash table to look up a value takes (on average) what time complexity?

Example 38

easy
What is the Big-O of three nested loops each running nn times?

Example 39

easy
A function does 3n+53n + 5 operations on an input of size nn. What is its Big-O time complexity?

Example 40

medium
A function has runtime T(n)=5n2+20n+100T(n) = 5n^2 + 20n + 100. What is its tightest Big-O?

Example 41

medium
What is the time complexity of finding the maximum of an unsorted array of nn elements?

Example 42

medium
What is the time complexity of bubble sort in the worst case?

Example 43

medium
What is the time complexity of computing the sum of all nn numbers in an array using a single loop?

Example 44

medium
What is the time complexity of looking up a value by index in an array (e.g., `a[i]`)?

Example 45

hard
What is the time complexity of the following snippet? `for i in 1..n: for j in 1..i: do_work()`

Example 46

hard
What is the time complexity of inserting at the front of an array of size nn?

Example 47

hard
A binary-search-tree lookup on a balanced tree has what time complexity? On a maximally unbalanced tree?

Background Knowledge

These ideas may be useful before you work through the harder examples.

algorithm