Time Complexity Questions with Answers (Big-O Practice)
Interviewers often show a loop and ask for its Big-O. Work out each one before revealing the answer.
Q1. What is the time complexity of this code, where size is the input size n?
low, high = 0, size - 1 while low <= high: mid = (low + high) // 2 high = mid - 1- A. O(2ⁿ)
- B. O(1)
- C. O(log n)
- D. O(n³)
Show answerHide answer
Answer: C. O(log n)
The range is halved every step, like binary search.Q2. What is the time complexity of this code, where size is the input size n?
for i in range(size): for j in range(size): for k in range(size): pass- A. O(n)
- B. O(n log n)
- C. O(√n)
- D. O(n³)
Show answerHide answer
Answer: D. O(n³)
Three nested loops of n each.Q3. What is the time complexity of this code, where n is the input size n?
low, high = 0, n - 1 while low <= high: mid = (low + high) // 2 high = mid - 1- A. O(n²)
- B. O(2ⁿ)
- C. O(n)
- D. O(log n)
Show answerHide answer
Answer: D. O(log n)
The range is halved every step, like binary search.Q4. What is the time complexity of this code, where n is the input size n?
for i in range(n): for j in range(n): print(i, j)- A. O(n²)
- B. O(1)
- C. O(√n)
- D. O(n log n)
Show answerHide answer
Answer: A. O(n²)
Two nested loops, each running n times.Q5. What is the time complexity of this code, where size is the input size n?
x = size * 2 + 7 print(x)
- A. O(n²)
- B. O(n³)
- C. O(1)
- D. O(√n)
Show answerHide answer
Answer: C. O(1)
A fixed number of steps regardless of n.Q6. What is the time complexity of this code, where count is the input size n?
for i in range(count): for j in range(10): print(i * j)- A. O(1)
- B. O(n)
- C. O(log n)
- D. O(n²)
Show answerHide answer
Answer: B. O(n)
The inner loop always runs 10 times (a constant), so it's 10n = O(n).Q7. What is the time complexity of this code, where size is the input size n?
for i in range(size): for j in range(i): print(j)- A. O(n³)
- B. O(n²)
- C. O(1)
- D. O(n)
Show answerHide answer
Answer: B. O(n²)
0 + 1 + … + (n−1) = n(n−1)/2 steps, which is O(n²).Q8. What is the time complexity of this code, where size is the input size n?
def fib(size): if size < 2: return size return fib(size - 1) + fib(size - 2)- A. O(n³)
- B. O(√n)
- C. O(2ⁿ)
- D. O(1)
Show answerHide answer
Answer: C. O(2ⁿ)
Each call makes two more calls, so the number of calls roughly doubles at every level.Q9. What is the time complexity of this code, where count is the input size n?
i = 1 while i < count: i = i * 2- A. O(n²)
- B. O(2ⁿ)
- C. O(log n)
- D. O(n³)
Show answerHide answer
Answer: C. O(log n)
i doubles each time, so the loop runs about log₂ n times.Q10. What is the time complexity of this code, where size is the input size n?
for i in range(size): for j in range(size): print(i, j)- A. O(log n)
- B. O(√n)
- C. O(n²)
- D. O(n)
Show answerHide answer
Answer: C. O(n²)
Two nested loops, each running n times.Q11. What is the time complexity of this code, where n is the input size n?
for i in range(n): j = 1 while j < n: j *= 2- A. O(n log n)
- B. O(√n)
- C. O(2ⁿ)
- D. O(n³)
Show answerHide answer
Answer: A. O(n log n)
The outer loop runs n times and the inner one log n times.Q12. What is the time complexity of this code, where n is the input size n?
def fib(n): if n < 2: return n return fib(n - 1) + fib(n - 2)- A. O(n³)
- B. O(2ⁿ)
- C. O(1)
- D. O(√n)
Show answerHide answer
Answer: B. O(2ⁿ)
Each call makes two more calls, so the number of calls roughly doubles at every level.
Practised these? Now prove it.
Take a timed mock test with new questions every attempt, earn a verified certificate at 70%+, and get noticed by employers.
Start a mock test