Array Traversal Patterns in DSA Java - Time & Space Complexity
When we go through an array, we want to know how long it takes as the array gets bigger.
We ask: How does the work grow when the array size grows?
Analyze the time complexity of the following code snippet.
public void printArray(int[] arr) {
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
}
This code goes through each item in the array and prints it once.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Accessing and printing each element in the array.
- How many times: Exactly once for each element, so as many times as the array length.
As the array gets bigger, the work grows in a straight line with the number of elements.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 prints |
| 100 | 100 prints |
| 1000 | 1000 prints |
Pattern observation: Doubling the array size doubles the work.
Time Complexity: O(n)
This means the time to finish grows directly with the number of items in the array.
[X] Wrong: "Since the loop is simple, it must be constant time O(1)."
[OK] Correct: The loop runs once for each element, so time grows with array size, not fixed.
Understanding how simple loops grow with input size helps you explain your code clearly and shows you know how to reason about efficiency.
"What if we nested another loop inside to compare each element with every other? How would the time complexity change?"
Practice
for (int i = 0; i < arr.length; i++) { System.out.print(arr[i] + " "); }Solution
Step 1: Understand the loop range
The loop starts at index 0 and runs while i is less than the array length, so it covers all valid indices from start to end.Step 2: Analyze the print statement inside the loop
Each element at index i is printed followed by a space, so all elements are printed in order.Final Answer:
Prints all elements of the array from start to end -> Option CQuick Check:
Loop from 0 to length-1 prints all elements [OK]
- Confusing loop direction and thinking it prints backwards
- Assuming it prints only one element
- Ignoring the loop condition
arr backwards in Java?Solution
Step 1: Identify correct start and end indices for backward traversal
Backward traversal starts at last index (arr.length - 1) and ends at 0 inclusive.Step 2: Check loop condition and decrement
The loop continues while i is greater than or equal to 0, decrementing i each time, correctly covering all indices backwards.Final Answer:
for (int i = arr.length - 1; i >= 0; i--) -> Option DQuick Check:
Start at last index, go down to 0 [OK]
- Starting loop at arr.length (out of bounds)
- Using i > 0 instead of i >= 0 (misses index 0)
- Starting loop at 1 instead of length-1
int[] arr = {2, 4, 6, 8};
for (int i = 1; i < arr.length; i += 2) {
System.out.print(arr[i] + " ");
}Solution
Step 1: Identify loop indices
The loop starts at i=1 and increments by 2, so i takes values 1 and 3 (since arr.length=4).Step 2: Print elements at indices 1 and 3
arr[1] = 4 and arr[3] = 8, so output is "4 8 "Final Answer:
4 8 -> Option AQuick Check:
Indices 1 and 3 printed: 4 and 8 [OK]
- Assuming loop prints all elements
- Confusing index increments
- Printing elements at even indices instead
int[] arr = {1, 3, 5, 7};
for (int i = arr.length; i >= 0; i--) {
System.out.print(arr[i] + " ");
}Solution
Step 1: Check starting index of loop
arr.length is 4, but valid indices are 0 to 3. Starting at 4 causes ArrayIndexOutOfBoundsException.Step 2: Correct starting index for backward traversal
Loop should start at arr.length - 1 (3) to access last element safely.Final Answer:
Loop starts at arr.length which is out of bounds -> Option AQuick Check:
Array indices go from 0 to length-1 [OK]
- Starting loop at arr.length instead of arr.length-1
- Ignoring index out of bounds error
- Confusing loop condition signs
arr, which code snippet correctly prints all elements at even indices in reverse order?Solution
Step 1: Determine the highest even index to start from
If array length is even, highest even index is length-2; if odd, it's length-1.Step 2: Loop backwards by 2 steps to cover even indices
Start from the highest even index and decrement by 2 to print all even indices in reverse order.Final Answer:
for (int i = (arr.length % 2 == 0 ? arr.length - 2 : arr.length - 1); i >= 0; i -= 2) { System.out.print(arr[i] + " "); } -> Option BQuick Check:
Start at highest even index, step down by 2 [OK]
- Starting at length-1 without checking even index
- Using i -= 1 instead of i -= 2
- Printing odd indices instead of even
Ready to prove it?
Pass the test and this pattern moves unknown
