Micro-questions over the loops module. Answer in a full sentence before opening the toggle. Format explained in the arrays bank.
A. Why loop counting matters
1. What is Big-O, at heart?
Answer
Simply counting how many times a loop repeats.
2. Give the 1,000-guest illustration of a single pass.
Answer
Handing a badge to each guest — 1,000 repetitions, about 15 minutes.
3. And the pairwise comparison?
Answer
Every guest introducing themselves to every other — handshakes, over 300 hours.
4. What is the point of the comparison?
Answer
Both operate on the exact same 1,000 guests, but the nested loop takes 500× longer.
B. The vocabulary
5. What is input size ?
Answer
The number of items your code has to process.
6. Define linear, quadratic and logarithmic time by what doubling does.
Answer
Linear : doubling the input doubles the time. Quadratic : doubling multiplies the time by 4. Logarithmic : 1,000 items → 10 steps; 1,000,000 → 20 steps.
7. What is the difference between a for and a while loop, in counting terms?
Answer
A for loop announces its iteration count upfront in its header. A while loop’s total depends on how variables change inside the body.
C. The four rules
8. Rule 1 — what do sequential loops do?
Answer
They add. , and constant multipliers are dropped, so it is .
9. Rule 2 — what do nested loops do, and why?
Answer
They multiply — the inner loop restarts completely for every single step of the outer loop. .
10. At , what does nesting cost?
Answer
1,000,000 iterations — which is why nesting is the number-one source of slow code.
11. Rule 3 — what about a triangular loop, for j in range(i, n)?
Answer
Total . Half as many steps as the full square, but dropping constants still leaves .
12. Rule 4 — what makes a loop logarithmic?
Answer
The loop variable being divided (or multiplied) by 2 each step, cutting the problem in half.
13. Give the three data points for why that is a superpower.
Answer
→ 10 steps; → 20 steps; → 30 steps.
14. What does halving make fast?
Answer
Binary search, balanced trees and heaps.
D. The hidden loop trap
15. State the warning.
Answer
An operation that reads as a single line of code may contain a secret loop under the hood.
16. What is hidden inside if item in seen_list:?
Answer
in on a list scans element by element — . Nested inside a for over items, the whole thing is .
17. What is the fix?
Answer
Swap the list for a set or dict — an lookup.
18. What is hidden inside list.insert(0, item)?
Answer
Every existing element shifts right by one — .
19. What is hidden inside str += char in a loop?
Answer
It rebuilds the entire string in memory each time — total. Fix: append to a list and ''.join() at the end.
20. What do all three hidden loops have in common?
Answer
They look like one operation and are actually a full pass — so the cost is invisible at the call site and only shows up in the total.
E. Traps
21. What causes an infinite while loop?
Answer
Forgetting to update the loop condition variable inside the body.
22. What is the range(n) off-by-one?
Answer
It produces indices 0 to n-1. Accessing arr[n] triggers an IndexError.
23. What happens if you modify a collection while iterating it?
Answer
You skip elements or get unpredictable crashes — the iterator’s position no longer means what it did.
24. Summarise the module in one sentence.
Answer
Count the repetitions: sequential loops add, nested loops multiply, halving loops are logarithmic — and check whether the one-line operations inside are loops too.
Scorecard
| Pass | Date | Got it | Close | Blank | Questions missed |
|---|---|---|---|---|---|
| 1 | |||||
| 2 | |||||
| 3 |
Related
- Loops and What They Cost — the module
- Growth & Asymptotic Notation — Question Bank
- Hash Maps — Question Bank — what removes the hidden lookup loop
Software engineering
Infrastructure & security
Computer science
Hardware & engineering