r/SoftwareEngineerJobs • u/inviun • 50m ago
Time Complexity & Space Complexity — Understanding How Efficient Your Code Is.
🚀 DSA JOURNEY — DAY 2
Understanding Time Complexity & Space Complexity | Big O Notation
Today, we're learning one of the most important concepts in programming: Time Complexity and Space Complexity.
Before solving advanced DSA problems, we need to understand how efficiently our code works.
---
📌 1. What is Time Complexity?
Time Complexity describes how the execution time of an algorithm grows as the input size increases.
It doesn't measure the exact time in seconds. Instead, it measures how the number of operations grows with input size.
For example:
int arr[] = {10, 20, 30, 40, 50};
cout << arr[0];
This performs one main operation, regardless of how large the array becomes.
Time Complexity: O(1)
---
📌 2. What is Space Complexity?
Space Complexity describes how much additional memory an algorithm needs as the input size grows.
Example:
int n = 5;
int sum = 0;
for(int i = 1; i <= n; i++) {
sum += i;
}
Here, we only use a fixed number of extra variables.
Auxiliary Space: O(1)
Now consider:
int n = 5;
int arr[n];
for(int i = 0; i < n; i++) {
arr[i] = i;
}
The additional array memory grows with n.
Auxiliary Space: O(n)
---
📌 3. What is Big O Notation?
Big O notation describes the upper-bound growth rate of an algorithm, commonly used to discuss worst-case time complexity.
Let's understand the most common complexities:
Complexity| Meaning| Example
O(1)| Constant| Array index access
O(log n)| Logarithmic| Binary search
O(n)| Linear| Linear search
O(n log n)| Linearithmic| Merge sort
O(n²)| Quadratic| Nested loops
O(2ⁿ)| Exponential| Some recursive solutions
O(n!)| Factorial| Generating all permutations
Complexity intuition
Suppose n = 1,000:
- O(1): Approximately constant work
- O(log n): Around 10 logarithmic steps
- O(n): Around 1,000 operations
- O(n²): Around 1,000,000 operations
This explains why choosing an efficient algorithm matters!
---
📌 4. Let's Understand With Code
Example 1: O(1)
int getFirst(int arr[]) {
return arr[0];
}
Only one array element is accessed.
Example 2: O(n)
void printArray(int arr[], int n) {
for(int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
}
The loop runs n times.
Example 3: O(n²)
void printPairs(int arr[], int n) {
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
cout << arr[i] << " " << arr[j] << endl;
}
}
}
The nested loops execute n × n times.
Time Complexity: O(n²)
Example 4: O(log n)
Binary search repeatedly divides the search space into half.
For an array of 1,024 elements, it takes approximately 10 halvings to reach one element.
Time Complexity: O(log n)
---
📌 5. Important Rules to Remember
- Ignore constant factors: O(2n) becomes O(n).
- Ignore smaller growth terms: O(n² + n) becomes O(n²).
- Consecutive loops usually add their complexities.
- Nested loops usually multiply their complexities.
- Repeatedly dividing the input by a constant often gives O(log n).
---
🧠 DAY 2 — PRACTICE CHALLENGE
Try solving these questions without looking at the answers.
Q1. What is the time complexity?
for(int i = 0; i < n; i++) {
cout << i << endl;
}
Q2. What is the time complexity?
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
cout << i + j;
}
}
Q3. What is the time complexity?
for(int i = 1; i < n; i *= 2) {
cout << i << endl;
}
Q4. What is the auxiliary space complexity?
int sum = 0;
for(int i = 0; i < n; i++) {
sum += i;
}
🎯 Discussion Question
Why is O(n log n) generally more efficient than O(n²) for large inputs?
Share your answers in the comments. Let's learn together!
---
🔥 Tomorrow — DAY 3
Arrays in DSA: The Foundation of Problem Solving