Counting operations and Big-O: Class 12 practice
Count how many times the innermost statement runs as a function of n. The quick answer and an example come first; then the method, worked examples and six graded questions.
Counting operations and Big-O: quick answer
Count how many times the innermost statement runs as a function of n. Single loops are O(n), nested loops over n are O(n²), and a loop that halves n each time is O(log n).
Example: for i in range(3 * n): x += 1. How many times does x += 1 run for n = 32, and what is the Big-O? Answer: 96; O(n).
- Where it is taught: "Algorithm Analysis", Class 12 Computer Science.
- On this page: the method, 3 worked examples and 6 graded questions with a full answer key.
How to use this page (about 30 minutes)
- Read the method once, then cover the worked examples and try them yourself before reading the steps.
- Do the two Basic questions without notes. If either is wrong, re-read the matching worked example.
- Do the Standard and Challenge questions, then check every line of working against the answer key — not only the final answer.
- Write down the mistake behind each wrong answer and redo that question the next day.
What you should know first
- Loops
The idea behind counting operations and big-o
Count how many times the innermost statement runs as a function of n. Single loops are O(n), nested loops over n are O(n²), and a loop that halves n each time is O(log n).
Step-by-step method
- Count iterations of each loop.
- Multiply nested counts.
- Keep the fastest-growing term for Big-O.
Worked example 1 (Basic)
for i in range(3 * n): x += 1. How many times does x += 1 run for n = 32, and what is the Big-O?
- 3 × 32 = 96
- O(n)
- Answer: 96; O(n)
Worked example 2 (Standard)
k = n; while k > 1: k = k // 2. How many iterations for n = 1000, and the Big-O?
- Halving 1000 until 1
- 9 iterations; O(log n)
- Answer: 9; O(log n)
Worked example 3 (Challenge)
for i in range(n): for j in range(i, n): ops += 1. Find ops for n = 200 and the Big-O.
- n + (n − 1) + … + 1 = n(n + 1)/2 = 20100
- O(n²)
- Answer: 20100; O(n²)
Common mistakes and misconceptions
- Calling a halving loop O(n)
- Keeping constants in Big-O
Practice questions
- (Basic) for i in range(5 * n): x += 1. How many times does x += 1 run for n = 500, and what is the Big-O?
- (Basic) for i in range(4 * n): x += 1. How many times does x += 1 run for n = 32, and what is the Big-O?
- (Standard) k = n; while k > 1: k = k // 2. How many iterations for n = 100, and the Big-O?
- (Standard) k = n; while k > 1: k = k // 2. How many iterations for n = 500, and the Big-O?
- (Challenge) for i in range(n): for j in range(i, n): ops += 1. Find ops for n = 64 and the Big-O.
- (Challenge) for i in range(n): for j in range(i, n): ops += 1. Find ops for n = 100 and the Big-O.
Answer key with full working
- Q1: 5 × 500 = 2500 → O(n) → Answer: 2500; O(n)
- Q2: 4 × 32 = 128 → O(n) → Answer: 128; O(n)
- Q3: Halving 100 until 1 → 6 iterations; O(log n) → Answer: 6; O(log n)
- Q4: Halving 500 until 1 → 8 iterations; O(log n) → Answer: 8; O(log n)
- Q5: n + (n − 1) + … + 1 = n(n + 1)/2 = 2080 → O(n²) → Answer: 2080; O(n²)
- Q6: n + (n − 1) + … + 1 = n(n + 1)/2 = 5050 → O(n²) → Answer: 5050; O(n²)
Which textbook this practice follows
This skill is taught under "Algorithm Analysis" in Class 12 Computer Science. Check your own textbook's contents page — CBSE, ICSE/ISC and state boards order chapters differently.
Answers on this page are generated and automatically re-checked by a second, independent calculation. They have not been reviewed by a teacher — if you spot a problem, tell us on WhatsApp.
Still stuck after the answer key?
If the same step in counting operations and big-o keeps going wrong, a short live 1 on 1 class can find the exact gap. The first demo class is free; send the class, board and chapter on WhatsApp (+91 9379416374).
Related practice and notes
More Class 12 Computer Science practice
Continue with this syllabus
Frequently asked questions
Which chapter is counting operations and big-o in?
This skill is taught under "Algorithm Analysis" in Class 12 Computer Science. Check your own textbook's contents page — CBSE, ICSE/ISC and state boards order chapters differently.
Which question should I start with?
Start with the Basic questions. Move to Standard only when both Basic answers match the key with the same working; the Challenge questions combine this skill with earlier ones.
How were the answers checked?
Answers on this page are generated and automatically re-checked by a second, independent calculation. They have not been reviewed by a teacher — if you spot a problem, tell us on WhatsApp.
Is this only for CBSE?
The skill names follow NCERT/CBSE, but the method is the same for ICSE, state boards, IGCSE and IB. Only the chapter name and order differ.
Can I get live help with this topic?
Yes. RG's E-Learning teaches live 1 on 1 online classes; the first demo class is free and fees are shared after it.