O(1) means constant time complexity. The time to complete the task does not change no matter how big the input is. For example, accessing a single item in a list by its index.
Click to reveal answer
beginner
Explain O(n) complexity with a real-life example.
O(n) means linear time complexity. The time grows directly with the size of the input. For example, checking each item in a shopping list one by one.
Click to reveal answer
intermediate
What does O(log n) complexity represent?
O(log n) means logarithmic time complexity. The time grows slowly as input size increases, often by cutting the problem in half each step. For example, finding a word in a dictionary by opening near the middle and narrowing down.
Click to reveal answer
intermediate
Describe O(n²) complexity and when it might happen.
O(n²) means quadratic time complexity. The time grows with the square of the input size. For example, comparing every student with every other student in a class to find pairs.
Click to reveal answer
beginner
Why is understanding complexity classes important?
Understanding complexity helps us predict how fast or slow an algorithm will run as data grows. It helps choose efficient solutions and avoid slow programs.
Click to reveal answer
Which complexity class means the running time does not change with input size?
AO(1)
BO(n)
CO(log n)
DO(n²)
✗ Incorrect
O(1) means constant time, so the running time stays the same regardless of input size.
If an algorithm checks every item in a list once, what is its complexity?
AO(n)
BO(1)
CO(log n)
DO(n²)
✗ Incorrect
Checking each item once means the time grows linearly with input size, which is O(n).
Which complexity is typical for binary search?
AO(n²)
BO(n)
CO(log n)
DO(1)
✗ Incorrect
Binary search cuts the search space in half each step, leading to O(log n) complexity.
What does O(n²) complexity imply about the number of operations?
AOperations grow linearly
BOperations grow logarithmically
COperations stay constant
DOperations grow with the square of input size
✗ Incorrect
O(n²) means the number of operations grows with the square of the input size.
Why should we avoid algorithms with high complexity for large data?
AThey run faster
BThey can become very slow and inefficient
CThey use less memory
DThey are easier to understand
✗ Incorrect
High complexity algorithms can become very slow as data grows, making them inefficient.
Explain the differences between O(1), O(n), O(log n), and O(n²) complexity classes with simple examples.
Think about how the time changes as input size grows.
You got /4 concepts.
Why is it important to understand complexity classes when choosing algorithms?
Consider the impact on speed and resources.
You got /4 concepts.
Practice
(1/5)
1. Which complexity class describes an algorithm that takes the same amount of time regardless of input size?
easy
A. O(n)
B. O(n²)
C. O(log n)
D. O(1)
Solution
Step 1: Understand what O(1) means
O(1) means constant time, so the algorithm's time does not change with input size.
Step 2: Compare with other options
O(n), O(log n), and O(n²) all depend on input size and grow as input grows.
Final Answer:
O(1) -> Option D
Quick Check:
Constant time = O(1) [OK]
Hint: Constant time means no input size effect [OK]
Common Mistakes:
Confusing O(1) with O(n)
Thinking O(log n) is constant
Mixing up growth rates
2. Which of the following is the correct way to describe an algorithm with time complexity that doubles the work when the input size doubles?
easy
A. O(log n)
B. O(n²)
C. O(n)
D. O(1)
Solution
Step 1: Understand linear growth
Doubling work when the input size doubles means time grows directly with input size, which is linear.
Step 2: Match with complexity classes
O(n) means time grows linearly with input size, so it fits the description.
Final Answer:
O(n) -> Option C
Quick Check:
Linear growth = O(n) [OK]
Hint: Linear means time grows directly with input size [OK]
Common Mistakes:
Choosing O(log n) for linear growth
Confusing O(n²) with linear
Thinking O(1) grows with input
3. Consider the following code snippet:
for i in range(n):
for j in range(n):
print(i, j)
What is the time complexity of this code?
medium
A. O(n)
B. O(n²)
C. O(log n)
D. O(1)
Solution
Step 1: Analyze the outer loop
The outer loop runs n times, from 0 to n-1.
Step 2: Analyze the inner loop
For each iteration of the outer loop, the inner loop runs n times.