Practice Algorithm Efficiency in CS Thinking

Use these practice problems to test your method after reviewing the concept explanation and worked examples.

Quick 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?

Showing a random 20 of 76 problems.

Example 1

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 2

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 3

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

Example 4

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 5

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?

Example 6

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

Example 7

easy
Big-O describes the worst-case growth rate of a function as input size approaches what?

Example 8

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 9

medium
Amortized analysis: appending to a dynamic array sometimes triggers a resize. What is the amortized cost per append?

Example 10

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

Example 11

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 12

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

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

hard
Rank from fastest to slowest growth for large nn: O(n!)O(n!), O(n3)O(n^3), O(logn)O(\log n), O(nlogn)O(n \log n), O(n)O(n).

Example 15

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

Example 16

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

Example 17

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 18

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

Example 19

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 20

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