Bird
Raised Fist0
DSA C++programming~20 mins

Array Reversal Techniques in DSA C++ - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Array Reversal Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of reversing an array using two-pointer swap
What is the output of the following C++ code that reverses an array using two pointers?
DSA C++
int arr[] = {1, 2, 3, 4, 5};
int n = 5;
int start = 0, end = n - 1;
while (start < end) {
    int temp = arr[start];
    arr[start] = arr[end];
    arr[end] = temp;
    start++;
    end--;
}
for (int i = 0; i < n; i++) {
    std::cout << arr[i] << " ";
}
A5 2 3 4 1
B1 2 3 4 5
C5 4 3 2 1
D4 3 2 1 5
Attempts:
2 left
💡 Hint
Think about swapping elements from the start and end moving towards the center.
❓ Predict Output
intermediate
2:00remaining
Output of reversing an array using recursion
What is the output of the following C++ recursive function that reverses an array?
DSA C++
void reverseArray(int arr[], int start, int end) {
    if (start >= end) return;
    int temp = arr[start];
    arr[start] = arr[end];
    arr[end] = temp;
    reverseArray(arr, start + 1, end - 1);
}

int main() {
    int arr[] = {10, 20, 30, 40};
    int n = 4;
    reverseArray(arr, 0, n - 1);
    for (int i = 0; i < n; i++) {
        std::cout << arr[i] << " ";
    }
}
A40 30 20 10
B10 20 30 40
C30 40 20 10
D40 20 30 10
Attempts:
2 left
💡 Hint
The function swaps outer elements and calls itself for inner elements.
🧠 Conceptual
advanced
1:30remaining
Time complexity of array reversal using two-pointer technique
What is the time complexity of reversing an array of size n using the two-pointer swapping technique?
AO(n)
BO(n^2)
CO(log n)
DO(1)
Attempts:
2 left
💡 Hint
Consider how many swaps happen relative to the array size.
🔧 Debug
advanced
2:00remaining
Identify the bug in this array reversal code
What error will occur when running this C++ code to reverse an array?
DSA C++
int arr[] = {1, 2, 3, 4, 5};
int n = 5;
int start = 0, end = n;
while (start < end) {
    int temp = arr[start];
    arr[start] = arr[end];
    arr[end] = temp;
    start++;
    end--;
}
for (int i = 0; i < n; i++) {
    std::cout << arr[i] << " ";
}
ALogical error: array remains unchanged
BNo error, prints reversed array
CCompilation error: missing semicolon
DRuntime error: out-of-bounds access
Attempts:
2 left
💡 Hint
Check the value of 'end' and array indexing.
🚀 Application
expert
2:30remaining
Reversing a subarray within an array
Given an array and two indices, start and end, which option correctly reverses only the subarray between start and end (inclusive)?
DSA C++
int arr[] = {5, 10, 15, 20, 25, 30};
int start = 1, end = 4;
// Reverse subarray from index 1 to 4
Awhile (start < end) { int temp = arr[start]; arr[start] = arr[end]; arr[end] = temp; start++; end--; }
Bfor (int i = 0; i < (end - start + 1) / 2; i++) { int temp = arr[start + i]; arr[start + i] = arr[end - i]; arr[end - i] = temp; }
Cstd::reverse(arr, arr + end - start + 1);
Dfor (int i = start; i <= end; i++) { int temp = arr[i]; arr[i] = arr[end - i + start]; arr[end - i + start] = temp; }
Attempts:
2 left
💡 Hint
Focus on swapping elements only within the subarray boundaries.

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 the meaning of reversal

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

    Sorting arranges elements by value, removing duplicates deletes repeated items, adding elements increases size. None of these change order like 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 order, 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 C++ code snippets correctly reverses an array arr of size n using a for loop?
easy
A. for(int i = 0; i < n; i++) { arr[i] = arr[i]; }
B. for(int i = 0; i < n; i++) { arr[i] = arr[n - i]; }
C. for(int i = 0; i < n / 2; i++) { std::swap(arr[i], arr[n - i - 1]); }
D. for(int i = 0; i < n; i++) { arr[i] = arr[i + 1]; }

Solution

  1. Step 1: Analyze the correct reversal loop

    The loop swaps elements from start and end moving towards the center, stopping at half the array size.
  2. Step 2: Check other options for errors

    A does nothing, for(int i = 0; i < n; i++) { arr[i] = arr[n - i]; } accesses out-of-bound index (arr[n - i]), C shifts elements incorrectly.
  3. Final Answer:

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

    Swap pairs from ends to middle [OK]
Hint: Swap pairs from ends to middle to reverse [OK]
Common Mistakes:
  • Accessing out-of-bound indices
  • Not stopping loop at half the array
  • Overwriting elements without swapping
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++) {
  std::cout << arr[i] << " ";
}
medium
A. 5 4 3 2 1
B. 1 2 3 4 5
C. 5 2 3 4 1
D. 4 3 2 1 5

Solution

  1. Step 1: Trace the swapping loop

    Swaps arr[0] with arr[4], arr[1] with arr[3]. After swaps, arr = {5,4,3,2,1}.
  2. Step 2: Print the array elements

    The loop prints all elements in order: 5 4 3 2 1.
  3. Final Answer:

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

    Reversed array prints 5 4 3 2 1 [OK]
Hint: Swapping pairs reverses array elements [OK]
Common Mistakes:
  • Not swapping correctly
  • Loop running too many times
  • Printing before reversal
4. Identify the error in the following code that attempts to reverse an array arr of size n:
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. No error; the code correctly reverses the array
B. The swap logic is incorrect; it should swap arr[i] with arr[i+1]
C. The loop should run from i = 1 to n / 2 instead of 0
D. The loop condition should be 'i < n / 2' to avoid swapping the middle element twice

Solution

  1. Step 1: Analyze the loop condition

    The loop runs while i <= n/2, which includes the middle element when n is odd.
  2. Step 2: Understand the effect of swapping middle element twice

    Swapping the middle element with itself is unnecessary and can cause incorrect behavior or redundant operation.
  3. Final Answer:

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

    Loop must stop before middle index [OK]
Hint: Use 'i < n/2' to avoid double swapping middle element [OK]
Common Mistakes:
  • Using <= instead of < in loop condition
  • Swapping wrong elements
  • Starting loop from 1 instead of 0
5. Given a 2D array (matrix) of size m x n, which approach correctly reverses each row individually in C++?
hard
A. Swap elements column-wise from first to last column for all rows simultaneously
B. Use a nested loop: for each row, swap elements from start to end using two pointers
C. Transpose the matrix and then reverse the entire matrix as a 1D array
D. Sort each row in descending order to reverse it

Solution

  1. Step 1: Understand reversing each row

    Reversing each row means flipping elements in that row only, not affecting other rows.
  2. Step 2: Identify correct method

    Using two pointers per row to swap elements from start and end is the correct approach.
  3. Step 3: Check incorrect options

    Swapping column-wise mixes rows, transposing changes structure, sorting changes order by value, not position.
  4. Final Answer:

    Use a nested loop: for each row, swap elements from start to end using two pointers -> Option B
  5. Quick Check:

    Reverse rows by swapping pairs inside each row [OK]
Hint: Reverse each row with two-pointer swaps inside nested loop [OK]
Common Mistakes:
  • Trying to reverse columns instead of rows
  • Using sorting instead of reversing
  • Transposing matrix instead of reversing rows
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