Introductions
Time and space complexity help us measure how efficiently an algorithm uses time and memory as the input size increases. In this chapter, you will practice identifying Big O, counting loop iterations, comparing algorithms, and calculating auxiliary space. The questions start with simple single-loop examples and gradually move to nested loops, logarithmic operations, and combinations of time and space complexity. Time and Space Complexity practice questions with solutions help to build concepts.
Question 1: Find the Time Complexity of a Single Loop
Question
What is the time complexity of the following code?
for (let i = 0; i < n; i++) {
console.log(i);
}
Solution
The loop starts at 0 and runs while:
i < n
The value of i increases by 1 each time.
So the loop executes approximately n times.
For example, if:
n = 5
The loop runs for:
0
1
2
3
4
That’s 5 iterations.
Therefore, the number of operations grows linearly with n.
Output
Time Complexity = O(n)
Answer
O(n)
This is called linear time complexity.
Question 2: Find the Time Complexity of a Constant Operation
Question
Find the time complexity of the following function:
function getFirstElement(arr) {
return arr[0];
}
Solution
The function accesses only the first element:
arr[0]
It does not matter whether the array contains:
5 elements
or:
5,000,000 elements
The function still performs one main operation.
Therefore, the execution time does not grow with the size of the input.
Output
Time Complexity = O(1)
Answer
O(1)
This is called constant time complexity.
Question 3: Find the Time Complexity of a Nested Loop
Question
Find the time complexity of this code:
for (let i = 0; i < n; i++) {
for (let j = 0; j < n; j++) {
console.log(i, j);
}
}
Solution
There are two loops.
The outer loop runs:
n times
For every iteration of the outer loop, the inner loop also runs:
n times
Therefore:
n × n
operations are performed.
So:
n × n = n²
When we convert this into Big O notation:
O(n²)
Output
Time Complexity = O(n²)
Answer
O(n²)
This is called quadratic time complexity.
Question 4: Find the Time Complexity When the Loop Doubles
Question
Find the time complexity of the following code:
let i = 1;
while (i < n) {
console.log(i);
i = i * 2;
}
Solution
The value of i doubles after every iteration.
The values look like:
1
2
4
8
16
32
64
...
Suppose:
n = 16
The loop runs approximately:
1 → 2 → 4 → 8
and then stops when the next value reaches 16.
The number of times we can double 1 before reaching n is approximately:
log₂(n)
Therefore, the time complexity is:
Output
Time Complexity = O(log n)
Answer
O(log n)
This is called logarithmic time complexity.
Question 5: Find the Time Complexity of Two Separate Loops
Question
Find the time complexity of this code:
for (let i = 0; i < n; i++) {
console.log(i);
}
for (let j = 0; j < n; j++) {
console.log(j);
}
Solution
There are two separate loops.
The first loop runs:
n times
The second loop also runs:
n times
Therefore, the total number of operations is:
n + n
which is:
2n
In Big O notation, constant multipliers are ignored.
Therefore:
O(2n) → O(n)
Output
Time Complexity = O(n)
Answer
O(n)
Remember: two separate loops that each run n times still give O(n), not O(n²).
Question 6: Find the Time Complexity of Different Loop Sizes
Question
Find the time complexity of the following code:
for (let i = 0; i < n; i++) {
console.log(i);
}
for (let j = 0; j < n * n; j++) {
console.log(j);
}
Solution
Let’s analyze each loop separately.
The first loop runs:
n times
So its complexity is:
O(n)
The second loop runs:
n × n
times.
Therefore:
O(n²)
The total complexity is:
O(n) + O(n²)
When different complexity terms are added, we keep the term that grows fastest.
Therefore:
O(n + n²)
becomes:
O(n²)
Output
Time Complexity = O(n²)
Answer
O(n²)
The n² operation dominates the n operation as the input becomes large.
Question 7: Find the Space Complexity
Question
What is the space complexity of this function?
function printNumbers(n) {
for (let i = 0; i < n; i++) {
console.log(i);
}
}
Solution
The loop runs n times, but it does not create a new array or another data structure that grows with n.
The function only uses a few variables:
n
i
The amount of extra memory remains approximately constant.
Therefore, the auxiliary space does not increase as n increases.
Output
Space Complexity = O(1)
Answer
O(1)
The loop takes O(n) time, but the function uses O(1) extra space.
Question 8: Find the Space Complexity of an Array
Question
Find the time and space complexity of the following function:
function createNumbers(n) {
let numbers = [];
for (let i = 0; i < n; i++) {
numbers.push(i);
}
return numbers;
}
Solution
First, look at the loop.
The loop runs:
n times
Therefore:
Time Complexity = O(n)
Now look at the array:
let numbers = [];
The array stores n values.
For example, if:
n = 5
the array becomes:
[0, 1, 2, 3, 4]
The memory required grows with n.
Therefore:
Space Complexity = O(n)
Output
Time Complexity = O(n)
Space Complexity = O(n)
Answer
Time: O(n)
Space: O(n)
Question 9: Compare Two Algorithms
Question
Two algorithms are given:
Algorithm A:
for (let i = 0; i < n; i++) {
console.log(i);
}
Algorithm B:
for (let i = 0; i < n; i++) {
for (let j = 0; j < n; j++) {
console.log(i, j);
}
}
Which algorithm is more efficient for large values of n?
Solution
First, calculate the complexity of Algorithm A.
It contains one loop that runs n times:
Algorithm A = O(n)
Now calculate Algorithm B.
It contains two nested loops:
n × n = n²
Therefore:
Algorithm B = O(n²)
Now compare:
O(n)
with:
O(n²)
As n becomes larger, n² grows much faster than n.
For example, when:
n = 100
Algorithm A performs roughly:
100 operations
while Algorithm B performs roughly:
10,000 operations
Output
Algorithm A → O(n)
Algorithm B → O(n²)
Answer
Algorithm A is more efficient for large input sizes.
Question 10: Find Both Time and Space Complexity
Question
Find the time and space complexity of the following function:
function findNumber(arr, target) {
let found = false;
for (let i = 0; i < arr.length; i++) {
if (arr[i] === target) {
found = true;
break;
}
}
return found;
}
Solution
Let’s analyze the time complexity first.
The function checks elements one by one.
In the worst case, the target may be:
- At the last position, or
- Not present in the array.
If the array contains n elements, the loop may run n times.
Therefore:
Time Complexity = O(n)
Now consider the extra memory.
The function uses only a few variables:
found
i
target
It does not create another array whose size depends on n.
Therefore:
Space Complexity = O(1)
Output
Time Complexity = O(n)
Space Complexity = O(1)
Answer
Time: O(n)
Space: O(1)
The algorithm may need to check every element, but it uses only a constant amount of additional memory.
Key Takeaways
- Time complexity describes how the running time of an algorithm grows as input size increases.
- Space complexity describes how much additional memory an algorithm needs.
- O(1) means constant complexity.
- O(n) means linear complexity.
- O(log n) means logarithmic complexity.
- O(n²) means quadratic complexity.
- A single loop that runs
ntimes is generally O(n). - Two nested loops that each run
ntimes are generally O(n²). - Two separate
nloops are generally O(n), not O(n²). - When adding complexity terms, the fastest-growing term usually determines the final Big O.
- An algorithm can have O(n) time and O(1) space.
- Creating an additional array containing
nelements generally requires O(n) space.
FAQs
1. What is Big O notation?
Big O notation describes how the time or space requirements of an algorithm grow as the input size increases.
2. What does O(1) mean?
O(1) means constant complexity. The amount of work or extra memory remains approximately the same regardless of the input size.
3. What does O(n) mean?
O(n) means linear complexity. If the input size increases, the amount of work generally increases proportionally.
4. What does O(n²) mean?
O(n²) means quadratic complexity. It commonly appears when an algorithm contains two nested loops that each depend on the input size.
5. Is O(n) better than O(n²)?
Yes, generally. For large input sizes, an O(n) algorithm grows much more slowly than an O(n²) algorithm.
6. What is the difference between time and space complexity?
Time complexity measures how the running time grows with input size, while space complexity measures how the additional memory requirement grows with input size.
7. Why is complexity important in data structures?
Complexity helps us compare different solutions and choose algorithms or data structures that can handle larger amounts of data efficiently.
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.
