Bird
Raised Fist0
DSA Cprogramming~10 mins

Array Reversal Techniques in DSA C - Execution Trace

Choose your learning style10 modes available
Concept Flow - Array Reversal Techniques
Start with array
↓
Set left = 0, right = length-1
↓
Check if left < right?
No→Done
Yes↓
Swap elements at left and right
↓
Increment left, Decrement right
↩Back to Check
The process swaps elements from the ends moving inward until the middle is reached.
Execution Sample
DSA C
int arr[] = {1, 2, 3, 4, 5};
int left = 0, right = 4;
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 start and end moving towards the center.
Execution Table
StepOperationleftrightArray StatePointer Changes
1Initialize left and right04[1, 2, 3, 4, 5]left=0, right=4
2Check left < right04[1, 2, 3, 4, 5]0 < 4 is True
3Swap arr[left] and arr[right]04[5, 2, 3, 4, 1]Swapped arr[0] and arr[4]
4Increment left, Decrement right13[5, 2, 3, 4, 1]left=1, right=3
5Check left < right13[5, 2, 3, 4, 1]1 < 3 is True
6Swap arr[left] and arr[right]13[5, 4, 3, 2, 1]Swapped arr[1] and arr[3]
7Increment left, Decrement right22[5, 4, 3, 2, 1]left=2, right=2
8Check left < right22[5, 4, 3, 2, 1]2 < 2 is False
9Done22[5, 4, 3, 2, 1]Reversal complete
💡 left is not less than right (2 < 2 is False), so loop ends
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 is not less than right?
Because when left equals or passes right, all pairs have been swapped. See execution_table rows 8 and 9 where 2 < 2 is False, so reversal is complete.
Why do we swap elements at left and right instead of just reversing by copying?
Swapping in place saves memory by not needing extra space. The execution_table shows how elements at positions left and right are exchanged step-by-step.
What happens if the array has an odd number of elements?
The middle element stays in place because left and right meet at the center. In the example, arr[2] = 3 remains unchanged as shown in the array state after step 7.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 6, what is the array state?
A[5, 4, 3, 2, 1]
B[5, 2, 3, 4, 1]
C[1, 2, 3, 4, 5]
D[1, 4, 3, 2, 5]
💡 Hint
Check the 'Array State' column at step 6 in the execution_table.
At which step does the condition left < right become false?
AStep 7
BStep 8
CStep 5
DStep 9
💡 Hint
Look at the 'Pointer Changes' and 'Operation' columns in execution_table rows 8 and 9.
If the array length was 6 instead of 5, how would the final left and right values differ?
Aleft=2, right=3
Bleft=2, right=2
Cleft=3, right=2
Dleft=3, right=3
💡 Hint
Consider how left increments and right decrements until left >= right for even length arrays.
Concept Snapshot
Array Reversal Technique:
- Use two pointers: left at start, right at end
- While left < right:
  - Swap arr[left] and arr[right]
  - Increment left, decrement right
- Stops when pointers meet or cross
- Works in-place without extra memory
Full Transcript
This visualization shows how to reverse an array by swapping elements from the ends moving inward. We start with two pointers, left at index 0 and right at the last index. While left is less than right, we swap the elements at these positions, then move left forward and right backward. This continues until left is no longer less than right, meaning the array is reversed. The example array [1, 2, 3, 4, 5] becomes [5, 4, 3, 2, 1] after the process. Key points include stopping when pointers meet or cross, and that the middle element in odd-length arrays stays in place. This method reverses the array in-place without extra memory.

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 order

    An array has elements arranged from first to last index.
  2. Step 2: Define reversal

    Reversing means swapping elements so the last element moves to the first position and vice versa.
  3. Final Answer:

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

    Reversal = Reverse order [OK]
Hint: Reversal flips the array order front to back [OK]
Common Mistakes:
  • Confusing reversal with sorting
  • Thinking reversal removes elements
  • Assuming reversal adds elements
2. Which of the following C code snippets correctly swaps two elements arr[i] and arr[j] for array reversal?
easy
A. arr[i] = arr[j]; arr[j] = arr[i];
B. temp = arr[j]; arr[j] = arr[i]; arr[i] = temp;
C. int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp;
D. arr[i] = arr[j]; arr[j] = temp;

Solution

  1. Step 1: Use a temporary variable for swapping

    To swap two values, store one in a temporary variable to avoid overwriting.
  2. Step 2: Assign values correctly

    Assign arr[i] to temp, then arr[j] to arr[i], then temp to arr[j].
  3. Final Answer:

    int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; -> Option C
  4. Quick Check:

    Swap uses temp variable [OK]
Hint: Always use a temp variable to swap two elements safely [OK]
Common Mistakes:
  • Overwriting one element before saving it
  • Using uninitialized temp variable
  • Swapping without temp causes data loss
3. What is the output of the following C code?
int arr[] = {1, 2, 3, 4, 5};
int n = 5;
for(int i = 0; i < n/2; i++) {
  int temp = arr[i];
  arr[i] = arr[n - i - 1];
  arr[n - i - 1] = temp;
}
for(int i = 0; i < n; i++) {
  printf("%d ", arr[i]);
}
medium
A. 2 1 4 3 5
B. 5 4 3 2 1
C. 1 2 3 4 5
D. 5 3 2 4 1

Solution

  1. Step 1: Understand the loop swaps

    The loop swaps elements from start and end moving towards the center: arr[0] with arr[4], arr[1] with arr[3].
  2. Step 2: Trace swaps

    Swap 1 and 5 -> arr = {5, 2, 3, 4, 1}; Swap 2 and 4 -> arr = {5, 4, 3, 2, 1}; middle element 3 stays same.
  3. Final Answer:

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

    Reversed array = 5 4 3 2 1 [OK]
Hint: Swapping pairs from ends reverses array [OK]
Common Mistakes:
  • Not swapping all pairs correctly
  • Loop running too many or too few times
  • Confusing indices in swap
4. Identify the error in this C code snippet intended to reverse an array:
int arr[] = {10, 20, 30, 40};
int n = 4;
for(int i = 0; i <= n/2; i++) {
  int temp = arr[i];
  arr[i] = arr[n - i - 1];
  arr[n - i - 1] = temp;
}
medium
A. Temporary variable temp is not declared
B. The swap logic is incorrect
C. Array size n is wrong
D. The loop condition should be i < n/2 instead of i <= n/2

Solution

  1. Step 1: Analyze loop condition

    The loop runs from i = 0 to i <= n/2, which means it runs one extra iteration when i = n/2.
  2. Step 2: Understand effect of extra iteration

    For even n=4, n/2=2, so i=2 is invalid index for swapping (out of bounds or swaps middle element twice).
  3. Final Answer:

    The loop condition should be i < n/2 instead of i <= n/2 -> Option D
  4. Quick Check:

    Loop runs too far, fix condition [OK]
Hint: Use i < n/2 to avoid extra swap [OK]
Common Mistakes:
  • Using <= causes out-of-bound swaps
  • Confusing array length with index
  • Ignoring zero-based indexing
5. Given an array arr of size n, which approach is best to reverse the array in-place with minimum extra memory and why?
hard
A. Use two pointers, one at start and one at end, swap and move towards center
B. Create a new array and copy elements from end to start
C. Use recursion to swap elements from ends
D. Sort the array in descending order

Solution

  1. Step 1: Compare memory usage of methods

    Creating a new array uses extra memory equal to array size; recursion uses call stack memory.
  2. Step 2: Evaluate two-pointer method

    Two pointers swap elements in-place without extra memory, making it efficient and simple.
  3. Final Answer:

    Use two pointers, one at start and one at end, swap and move towards center -> Option A
  4. Quick Check:

    Two-pointer swap = minimal memory [OK]
Hint: Two-pointer swap reverses in-place with no extra memory [OK]
Common Mistakes:
  • Using extra arrays wastes memory
  • Recursion adds unnecessary stack overhead
  • Sorting does not reverse original order
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