While Time Complexity measures how much time an algorithm takes as input grows, Space Complexity measures how much extra RAM (Memory) the algorithm requires.
In competitive programming and interviews, Space Complexity is often referred to as Auxiliary Space. This is the extra space you declare temporarily to solve the problem (like creating a new array or hash map inside the method). It does NOT include the memory taken up by the original input array passed into your function.
Memory is cheap today, but not infinite. If an algorithm requires O(N^2) space to process 10,000 items, it will try to allocate 100 million integers, which might crash a mobile app or server. Writing algorithms that run in O(1) space (doing the work 'in-place') is a highly prized engineering skill.
public class SpaceComplexity {
// 1. O(1) Space - "In-Place"
// We only create primitive variables (i, temp).
// They take the same memory regardless of arr.length.
public void reverseInPlace(int[] arr) {
int left = 0;
int right = arr.length - 1;
while (left < right) {
int temp = arr[left];
arr[left] = arr[right];
arr[right] = temp;
left++; right--;
}
}
// 2. O(N) Space - "Linear Space"
// We create a BRAND NEW array exactly the size of the input.
// If the input doubles, our RAM usage doubles.
public int[] copyArray(int[] arr) {
int[] result = new int[arr.length];
for (int i = 0; i < arr.length; i++) {
result[i] = arr[i];
}
return result;
}
// 3. O(N^2) Space - "Quadratic Space"
// We create a 2D Matrix (Grid). If input is size 10, we make 100 slots.
// If input is size 100, we make 10,000 slots! Very dangerous!
public int[][] buildMatrix(int[] arr) {
int[][] matrix = new int[arr.length][arr.length];
return matrix;
}
}Why do we care about O(1) space complexity?
O(1) algorithms are called 'In-Place' algorithms. They manipulate the existing data structures without requiring any significant extra RAM. This is crucial for embedded systems, mobile devices, and massive datasets where duplicating the data (O(N) space) would cause an OutOfMemory crash.
What is the Space Complexity of Merge Sort vs Quick Sort?
Merge Sort requires O(N) auxiliary space because it must create temporary arrays to merge the halves together. Quick Sort operates In-Place, swapping elements within the original array, but requires O(log N) space for the recursive call stack. Thus, Quick Sort is usually preferred for its better space efficiency.
When processing a massive 50GB log file, you cannot load it into a String[] array (O(N) space). Your computer will crash. Instead, you read it line-by-line using a BufferedReader, process a single string, and discard it before reading the next. This operates in O(1) space, allowing you to process infinite amounts of data.
// O(1) Space Complexity File Processing
public void searchLogFile(String filePath, String errorCode) {
// Only one line exists in memory at any given time
try (BufferedReader br = new BufferedReader(new FileReader(filePath))) {
String line;
while ((line = br.readLine()) != null) {
if (line.contains(errorCode)) {
System.out.println("Found: " + line);
}
}
}
}Test your understanding of Big O Space Complexity with these quick questions.
Question 1
If an algorithm creates two integer variables (`i` and `j`) to iterate through an array of 1 million items, what is the Auxiliary Space Complexity?
Question 2
Does a recursive algorithm use extra space?
Question 3
What does 'In-Place' algorithm mean?
While Time Complexity measures how much time an algorithm takes as input grows, Space Complexity measures how much extra RAM (Memory) the algorithm requires.
In competitive programming and interviews, Space Complexity is often referred to as Auxiliary Space. This is the extra space you declare temporarily to solve the problem (like creating a new array or hash map inside the method). It does NOT include the memory taken up by the original input array passed into your function.
Memory is cheap today, but not infinite. If an algorithm requires O(N^2) space to process 10,000 items, it will try to allocate 100 million integers, which might crash a mobile app or server. Writing algorithms that run in O(1) space (doing the work 'in-place') is a highly prized engineering skill.
public class SpaceComplexity {
// 1. O(1) Space - "In-Place"
// We only create primitive variables (i, temp).
// They take the same memory regardless of arr.length.
public void reverseInPlace(int[] arr) {
int left = 0;
int right = arr.length - 1;
while (left < right) {
int temp = arr[left];
arr[left] = arr[right];
arr[right] = temp;
left++; right--;
}
}
// 2. O(N) Space - "Linear Space"
// We create a BRAND NEW array exactly the size of the input.
// If the input doubles, our RAM usage doubles.
public int[] copyArray(int[] arr) {
int[] result = new int[arr.length];
for (int i = 0; i < arr.length; i++) {
result[i] = arr[i];
}
return result;
}
// 3. O(N^2) Space - "Quadratic Space"
// We create a 2D Matrix (Grid). If input is size 10, we make 100 slots.
// If input is size 100, we make 10,000 slots! Very dangerous!
public int[][] buildMatrix(int[] arr) {
int[][] matrix = new int[arr.length][arr.length];
return matrix;
}
}Why do we care about O(1) space complexity?
O(1) algorithms are called 'In-Place' algorithms. They manipulate the existing data structures without requiring any significant extra RAM. This is crucial for embedded systems, mobile devices, and massive datasets where duplicating the data (O(N) space) would cause an OutOfMemory crash.
What is the Space Complexity of Merge Sort vs Quick Sort?
Merge Sort requires O(N) auxiliary space because it must create temporary arrays to merge the halves together. Quick Sort operates In-Place, swapping elements within the original array, but requires O(log N) space for the recursive call stack. Thus, Quick Sort is usually preferred for its better space efficiency.
When processing a massive 50GB log file, you cannot load it into a String[] array (O(N) space). Your computer will crash. Instead, you read it line-by-line using a BufferedReader, process a single string, and discard it before reading the next. This operates in O(1) space, allowing you to process infinite amounts of data.
// O(1) Space Complexity File Processing
public void searchLogFile(String filePath, String errorCode) {
// Only one line exists in memory at any given time
try (BufferedReader br = new BufferedReader(new FileReader(filePath))) {
String line;
while ((line = br.readLine()) != null) {
if (line.contains(errorCode)) {
System.out.println("Found: " + line);
}
}
}
}Test your understanding of Big O Space Complexity with these quick questions.
Question 1
If an algorithm creates two integer variables (`i` and `j`) to iterate through an array of 1 million items, what is the Auxiliary Space Complexity?
Question 2
Does a recursive algorithm use extra space?
Question 3
What does 'In-Place' algorithm mean?