Bird
Raised Fist0
DSA Javaprogramming~10 mins

Array Reversal Techniques in DSA Java - Execution Trace

Choose your learning style10 modes available
Concept Flow - Array Reversal Techniques
Start with array
↓
Set two pointers: left=0, right=length-1
↓
Check if left < right?
No→Done
Yes↓
Swap elements at left and right
↓
Move left++ and right--
↩Back to check left < right
We use two pointers at the ends of the array, swap their elements, then move inward until they meet or cross.
Execution Sample
DSA Java
int[] arr = {1, 2, 3, 4, 5};
int left = 0, right = arr.length - 1;
while (left < right) {
  int temp = arr[left];
  arr[left] = arr[right];
  arr[right] = temp;
  left++;
  right--;
}
This code reverses the array by swapping elements from the ends moving towards the center.
Execution Table
StepOperationleftrightSwap ElementsArray State
1Initialize pointers04None[1, 2, 3, 4, 5]
2Check left < right04Yes[1, 2, 3, 4, 5]
3Swap arr[0] and arr[4]04Swap 1 and 5[5, 2, 3, 4, 1]
4Increment left, decrement right13None[5, 2, 3, 4, 1]
5Check left < right13Yes[5, 2, 3, 4, 1]
6Swap arr[1] and arr[3]13Swap 2 and 4[5, 4, 3, 2, 1]
7Increment left, decrement right22None[5, 4, 3, 2, 1]
8Check left < right22No[5, 4, 3, 2, 1]
💡 left equals right (2 == 2), so loop ends; array is reversed.
Variable Tracker
VariableStartAfter Step 3After Step 4After Step 6After Step 7Final
left001122
right443322
arr[1, 2, 3, 4, 5][5, 2, 3, 4, 1][5, 2, 3, 4, 1][5, 4, 3, 2, 1][5, 4, 3, 2, 1][5, 4, 3, 2, 1]
Key Moments - 3 Insights
Why do we stop when left equals right?
Because when left == right, it means we reached the middle element in an odd-length array, which does not need swapping. See execution_table row 8 where left=2 and right=2, loop ends.
Why do we swap elements at left and right pointers?
Swapping arr[left] and arr[right] moves the element from the start to the end and vice versa, gradually reversing the array. Refer to execution_table rows 3 and 6 for swaps.
What happens if we forget to increment left and decrement right?
The pointers won't move inward, causing an infinite loop. The array won't reverse. See execution_table rows 4 and 7 where pointers move to avoid this.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3, what is the array state after swapping?
A[5, 2, 3, 4, 1]
B[1, 2, 3, 4, 5]
C[5, 4, 3, 2, 1]
D[1, 4, 3, 2, 5]
💡 Hint
Check the 'Array State' column at step 3 in execution_table.
At which step does the condition left < right become false?
AStep 6
BStep 8
CStep 4
DStep 2
💡 Hint
Look at the 'Check left < right' operation and the condition result in execution_table.
If the array length was even, how would the final pointers' positions differ?
Aleft would be zero and right would be length-1
Bleft would equal right when loop ends
Cleft would be greater than right when loop ends
Dleft and right would never change
💡 Hint
Refer to variable_tracker and think about how pointers move inward for even length arrays.
Concept Snapshot
Array Reversal using two pointers:
- Initialize left=0, right=length-1
- While left < right:
  Swap arr[left] and arr[right]
  Increment left, decrement right
- Stops when pointers meet or cross
- Result: array reversed in place
Full Transcript
This visualization shows how to reverse an array by swapping elements from the ends moving inward. Two pointers, left and right, start at the beginning and end of the array. While left is less than right, the elements at these positions are swapped. Then left moves right, and right moves left. This continues until the pointers meet or cross, meaning the array is reversed. The execution table tracks each step, showing pointer positions, swaps, and array state. Key moments clarify why the loop stops and why pointer movement is essential. The quiz tests understanding of array states and pointer logic.

Practice

(1/5)
1. What does reversing an array mean in simple terms?
easy
A. Changing the order of elements so the last becomes first and first becomes last
B. Sorting the array in ascending order
C. Removing duplicate elements from the array
D. Adding new elements at the end of the array

Solution

  1. Step 1: Understand array reversal meaning

    Reversing means flipping the order of elements so the last element comes first and the first element comes last.
  2. Step 2: Compare with other options

    Sorting arranges elements by value, removing duplicates deletes repeated items, and adding elements is insertion, not reversal.
  3. Final Answer:

    Changing the order of elements so the last becomes first and first becomes last -> Option A
  4. Quick Check:

    Reversal = Flip order [OK]
Hint: Reversal flips array ends, not sorting or removing [OK]
Common Mistakes:
  • Confusing reversal with sorting
  • Thinking reversal removes elements
  • Mixing reversal with adding elements
2. Which of the following Java code snippets correctly reverses an integer array arr in-place?
easy
A. for(int i = arr.length - 1; i >= 0; i--) { System.out.print(arr[i]); }
B. for(int i = 0; i < arr.length; i++) { arr[i] = arr[arr.length - 1 - i]; }
C. for(int i = 0; i < arr.length / 2; i++) { int temp = arr[i]; arr[i] = arr[arr.length - 1 - i]; arr[arr.length - 1 - i] = temp; }
D. Arrays.sort(arr);

Solution

  1. Step 1: Analyze in-place reversal logic

    for(int i = 0; i < arr.length / 2; i++) { int temp = arr[i]; arr[i] = arr[arr.length - 1 - i]; arr[arr.length - 1 - i] = temp; } swaps elements from start and end moving towards center, correctly reversing the array in-place.
  2. Step 2: Check other options

    for(int i = 0; i < arr.length; i++) { arr[i] = arr[arr.length - 1 - i]; } overwrites elements incorrectly, for(int i = arr.length - 1; i >= 0; i--) { System.out.print(arr[i]); } only prints reversed array without changing it, Arrays.sort(arr); sorts array, not reverses.
  3. Final Answer:

    for(int i = 0; i < arr.length / 2; i++) { int temp = arr[i]; arr[i] = arr[arr.length - 1 - i]; arr[arr.length - 1 - i] = temp; } -> Option C
  4. Quick Check:

    Swap pairs from ends = reversal [OK]
Hint: Swap pairs from ends up to middle for reversal [OK]
Common Mistakes:
  • Overwriting elements without swapping
  • Only printing reversed array without changing it
  • Using sort instead of reversal
3. What will be the output of the following Java code?
int[] arr = {1, 2, 3, 4, 5};
for(int i = 0; i < arr.length / 2; i++) {
  int temp = arr[i];
  arr[i] = arr[arr.length - 1 - i];
  arr[arr.length - 1 - i] = temp;
}
for(int num : arr) {
  System.out.print(num + " ");
}
medium
A. 5 4 3 2 1
B. 1 2 3 4 5
C. 5 2 3 4 1
D. 1 4 3 2 5

Solution

  1. Step 1: Trace the reversal loop

    The loop swaps arr[0] with arr[4], arr[1] with arr[3]. After swaps, array becomes [5,4,3,2,1].
  2. Step 2: Print the array elements

    The print loop outputs elements in order: 5 4 3 2 1
  3. Final Answer:

    5 4 3 2 1 -> Option A
  4. Quick Check:

    Swapped ends = reversed array [OK]
Hint: Swapping ends reverses array; print shows reversed order [OK]
Common Mistakes:
  • Not swapping correctly, leaving array unchanged
  • Confusing print output with original array
  • Off-by-one errors in loop
4. Identify the error in the following Java code that attempts to reverse an array arr:
for(int i = 0; i <= arr.length / 2; i++) {
  int temp = arr[i];
  arr[i] = arr[arr.length - 1 - i];
  arr[arr.length - 1 - i] = temp;
}
medium
A. The temp variable should be declared outside the loop
B. The loop condition should be 'i < arr.length / 2' to avoid swapping middle element twice
C. The swap logic is incorrect; it should swap arr[i] with arr[i+1]
D. The loop should run from 'i = arr.length - 1' down to 0

Solution

  1. Step 1: Analyze loop condition

    The loop uses 'i <= arr.length / 2', which causes the middle element to be swapped twice when array length is odd.
  2. Step 2: Correct loop condition

    Changing condition to 'i < arr.length / 2' ensures swaps happen only once per pair, avoiding double swap of middle element.
  3. Final Answer:

    The loop condition should be 'i < arr.length / 2' to avoid swapping middle element twice -> Option B
  4. Quick Check:

    Loop limit excludes middle for odd length [OK]
Hint: Use '<' not '<=' to avoid double swapping middle element [OK]
Common Mistakes:
  • Using <= instead of < in loop condition
  • Declaring temp outside loop unnecessarily
  • Incorrect swap logic swapping wrong elements
5. Given a 2D integer array matrix of size n x n, which code snippet correctly reverses each row of the matrix in-place?
hard
A. for(int i = 0; i < n; i++) { Arrays.sort(matrix[i]); }
B. for(int i = 0; i < n; i++) { for(int j = 0; j < n; j++) { matrix[i][j] = matrix[i][n - 1 - j]; } }
C. for(int j = 0; j < n; j++) { for(int i = 0; i < n / 2; i++) { int temp = matrix[i][j]; matrix[i][j] = matrix[n - 1 - i][j]; matrix[n - 1 - i][j] = temp; } }
D. for(int i = 0; i < n; i++) { for(int j = 0; j < n / 2; j++) { int temp = matrix[i][j]; matrix[i][j] = matrix[i][n - 1 - j]; matrix[i][n - 1 - j] = temp; } }

Solution

  1. Step 1: Understand reversing each row

    Reversing each row means swapping elements in each row from start to end moving towards center.
  2. Step 2: Analyze options

    for(int i = 0; i < n; i++) { for(int j = 0; j < n / 2; j++) { int temp = matrix[i][j]; matrix[i][j] = matrix[i][n - 1 - j]; matrix[i][n - 1 - j] = temp; } } swaps elements in each row correctly. for(int i = 0; i < n; i++) { for(int j = 0; j < n; j++) { matrix[i][j] = matrix[i][n - 1 - j]; } } overwrites elements incorrectly. for(int j = 0; j < n; j++) { for(int i = 0; i < n / 2; i++) { int temp = matrix[i][j]; matrix[i][j] = matrix[n - 1 - i][j]; matrix[n - 1 - i][j] = temp; } } swaps columns, not rows. for(int i = 0; i < n; i++) { Arrays.sort(matrix[i]); } sorts rows, not reverses.
  3. Final Answer:

    for(int i = 0; i < n; i++) { for(int j = 0; j < n / 2; j++) { int temp = matrix[i][j]; matrix[i][j] = matrix[i][n - 1 - j]; matrix[i][n - 1 - j] = temp; } } -> Option D
  4. Quick Check:

    Swap pairs in each row for reversal [OK]
Hint: Reverse each row by swapping pairs from ends to center [OK]
Common Mistakes:
  • Swapping columns instead of rows
  • Overwriting elements without swapping
  • Sorting rows instead of reversing
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard