1. Nested loopsEasy
To find customers on both the email list (a, n items) and the SMS list (b, m items), this checks every pair. What is the time complexity?
for x in a:
for y in b:
if x == y:
print(x)
- O(n²)
- O(n + m)
- O(n · m)
- O(n log m)
Show answer
Answer: C. O(n · m)
The inner loop runs m times for each of the n outer iterations. Writing O(n²) is a common slip; it's only right if both inputs are the same size. A set of b would make it O(n + m).
2. Sequential loopsEasy
A report prints every order (a, n items), then every refund (b, m items). What is the time complexity?
for x in a:
print(x)
for y in b:
print(y)
- O(n)
- O(n + m)
- O(n · m)
- O(max(n, m)²)
Show answer
Answer: B. O(n + m)
Loops one after another add; loops nested inside each other multiply. Keep both variables: you can't drop m when the inputs are independent.
3. Logarithmic loopsEasy
A pagination helper keeps halving the range n until one item is left. What is the time complexity?
while n > 1:
n //= 2
- O(1)
- O(log n)
- O(n)
- O(n / 2)
Show answer
Answer: B. O(log n)
Halving n each step reaches 1 after about log₂ n steps. Any loop that divides (or multiplies) by a constant is logarithmic.
4. ConstantsEasy
Every user gets exactly 5 notification checks. What's the time complexity?
for i in range(n):
for j in range(5):
total += i * j
- O(n)
- O(n²)
- O(5n²)
- O(n log n)
Show answer
Answer: A. O(n)
The inner loop always runs 5 times regardless of n: 5n operations, which is O(n). Nesting only multiplies growth when the inner bound grows with the input.
5. Nested loopsEasy
Comparing every pair of users for duplicates, without repeats. What's the time complexity?
for i in range(n):
for j in range(i, n):
total += 1
- O(n)
- O(n log n)
- O(n²)
- O(2ⁿ)
Show answer
Answer: C. O(n²)
The inner loop runs n, n-1, …, 1 times: n(n+1)/2 iterations in total. Dropping constants gives O(n²). Halving the work doesn't change the growth rate.
6. GraphsEasy
A friend-suggestion feature runs BFS over a social graph stored as adjacency lists: V users, E friendships. What's its time complexity?
- O(V)
- O(E)
- O(V + E)
- O(V · E)
Show answer
Answer: C. O(V + E)
Each vertex is enqueued once and each edge is examined once (twice for undirected). With an adjacency matrix it becomes O(V²), because finding neighbours scans a whole row.
7. Data structuresEasy
A loop runs x in items a million times. In Python, what's the average cost of each check when items is a list, and when it's a set?
- O(1) and O(1)
- O(n) and O(1)
- O(log n) and O(1)
- O(n) and O(log n)
Show answer
Answer: B. O(n) and O(1)
A list is scanned element by element. A set is a hash table: hash x, jump to the bucket. Turning a list into a set first is a common interview optimization.
8. SortingEasy
You pick merge sort for 2 GB of log lines on a box with 3 GB of RAM. What are its time and extra space complexity?
- O(n log n) time, O(1) space
- O(n log n) time, O(n) space
- O(n²) time, O(1) space
- O(n) time, O(n) space
Show answer
Answer: B. O(n log n) time, O(n) space
It always splits in half (log n levels) and does O(n) merging work per level. Merging needs a temporary buffer, hence O(n) space. Heapsort is O(n log n) with O(1) space but isn't stable.
9. StringsMedium
This Java export builds a CSV with 200,000 rows. Which review comment matters most?
String csv = "";
for (Row r : rows) {
csv += r.toCsv() + "\n";
}
- Use a ternary to build each line instead
- Each
+= copies the whole string so far: O(n²); use a StringBuilder - Rename
csv to output for clarity - Use
\r\n line endings so Excel opens it correctly
Show answer
Answer: B. Each += copies the whole string so far: O(n²); use a StringBuilder
Every concatenation allocates a new string and copies the old one. With 200,000 rows that's billions of character copies. StringBuilder.append grows a buffer, so the total work is linear.
10. Combining stepsMedium
You sort n items once, then answer n lookups with binary search. Total time?
- O(n)
- O(n log n)
- O(n² log n)
- O(log n)
Show answer
Answer: B. O(n log n)
Sorting is O(n log n). n binary searches are n × O(log n) = O(n log n). Adding two O(n log n) terms is still O(n log n).
11. SortingMedium
Quicksort always picks the first element as the pivot. What is its time on an already-sorted array?
- O(n)
- O(n log n)
- O(n²)
- O(log n)
Show answer
Answer: C. O(n²)
Each partition peels off just one element, so there are n levels of O(n) work. Random or median-of-three pivots make this vanishingly unlikely.
12. Hidden loopsMedium
As the business grew 10×, this nightly job got 100× slower. Why?
vip_ids = [v.id for v in vips]
for c in customers:
if c.id in vip_ids:
tag(c)
tag is slow, and it now runs 10× more oftenin on a list scans it: O(customers × vips), both grew 10×- Building
vip_ids with a comprehension is quadratic - Python slows down as memory use grows
Show answer
Answer: B. in on a list scans it: O(customers × vips), both grew 10×
10× more customers × 10× more VIPs = 100× more comparisons. Make vip_ids a set ({v.id for v in vips}) and each lookup is O(1) on average, so the job is linear again.
13. Space complexityMedium
A DP solution for the longest common subsequence of strings with lengths n and m fills a full table. How much space does it use, and how low can it go if you only need the length?
- O(n · m), down to O(min(n, m))
- O(n + m), down to O(1)
- O(n · m), can't be reduced
- O(2ⁿ), down to O(n)
Show answer
Answer: A. O(n · m), down to O(min(n, m))
Each row only depends on the previous one, so you can keep two rows (or one, carefully) of the shorter string's length. You lose the ability to reconstruct the actual subsequence.
14. RecursionMedium
A teammate's recursive Fibonacci returns instantly for n = 30 but seems to hang at n = 50. What's its time complexity?
def fib(n):
if n < 2:
return n
return fib(n - 1) + fib(n - 2)
- O(n)
- O(n²)
- O(2ⁿ)
- O(n log n)
Show answer
Answer: C. O(2ⁿ)
Each call branches into two more, and the same values are recomputed over and over. The tight bound is about O(1.618ⁿ), usually stated as O(2ⁿ). Memoization brings it down to O(n).
15. Back-of-envelopeMedium
Your O(n²) solution runs on n = 100,000, and the machine does about 10⁸ simple operations per second. Roughly how long does it take?
- About 0.1 seconds
- About 1 second
- About 100 seconds
- About 3 hours
Show answer
Answer: C. About 100 seconds
n² = 10¹⁰ operations ÷ 10⁸ per second = 100 seconds. That's why judges with a 1-2 s limit force O(n log n) at this size. O(n log n) would be about 1.7 × 10⁶ operations: milliseconds.