Bird
Raised Fist0
DSA Cprogramming~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 Array Reversal Using Two-Pointer Swap
What is the output of the following C code that reverses an array using two pointers?
DSA C
#include <stdio.h>

void reverse(int arr[], int n) {
    int start = 0, end = n - 1;
    while (start < end) {
        int temp = arr[start];
        arr[start] = arr[end];
        arr[end] = temp;
        start++;
        end--;
    }
}

int main() {
    int arr[] = {1, 2, 3, 4, 5};
    int n = 5;
    reverse(arr, n);
    for (int i = 0; i < n; i++) {
        printf("%d ", arr[i]);
    }
    return 0;
}
A1 2 3 4 5
B1 5 3 2 4
C5 4 3 2 1
D5 2 3 4 1
Attempts:
2 left
💡 Hint
Think about how swapping elements from start and end moves towards the center.
❓ Predict Output
intermediate
2:00remaining
Output of Array Reversal Using Recursion
What is the output of this C program that reverses an array recursively?
DSA C
#include <stdio.h>

void reverse(int arr[], int start, int end) {
    if (start >= end) return;
    int temp = arr[start];
    arr[start] = arr[end];
    arr[end] = temp;
    reverse(arr, start + 1, end - 1);
}

int main() {
    int arr[] = {10, 20, 30, 40};
    int n = 4;
    reverse(arr, 0, n - 1);
    for (int i = 0; i < n; i++) {
        printf("%d ", arr[i]);
    }
    return 0;
}
A10 20 30 40
B40 30 20 10
C40 20 30 10
D10 40 30 20
Attempts:
2 left
💡 Hint
Recursion swaps outer elements and moves inward.
🔧 Debug
advanced
2:00remaining
Identify the Error in Array Reversal Code
What error does the following C code produce when trying to reverse an array?
DSA C
#include <stdio.h>

void reverse(int arr[], int n) {
    int i;
    for (i = 0; i <= n / 2; i++) {
        int temp = arr[i];
        arr[i] = arr[n - i];
        arr[n - i] = temp;
    }
}

int main() {
    int arr[] = {1, 2, 3, 4};
    int n = 4;
    reverse(arr, n);
    for (int i = 0; i < n; i++) {
        printf("%d ", arr[i]);
    }
    return 0;
}
ARuntime error: Array index out of bounds
BNo error, prints reversed array correctly
CCompilation error: missing semicolon
DLogical error: array remains unchanged
Attempts:
2 left
💡 Hint
Check the index used for swapping elements carefully.
❓ Predict Output
advanced
2:00remaining
Output of Array Reversal Using XOR Swap
What is the output of this C code that reverses an array using XOR swap technique?
DSA C
#include <stdio.h>

void reverse(int arr[], int n) {
    int start = 0, end = n - 1;
    while (start < end) {
        arr[start] ^= arr[end];
        arr[end] ^= arr[start];
        arr[start] ^= arr[end];
        start++;
        end--;
    }
}

int main() {
    int arr[] = {7, 8, 9};
    int n = 3;
    reverse(arr, n);
    for (int i = 0; i < n; i++) {
        printf("%d ", arr[i]);
    }
    return 0;
}
A9 7 8
B8 7 9
C7 8 9
D9 8 7
Attempts:
2 left
💡 Hint
XOR swap exchanges values without a temporary variable.
🧠 Conceptual
expert
2:00remaining
Time Complexity of Different Array Reversal Techniques
Which statement correctly describes the time complexity of common array reversal methods (two-pointer swap, recursion, XOR swap)?
AAll methods have O(n) time complexity because each element is visited once.
BRecursion method has O(n^2) time complexity due to repeated calls.
CXOR swap method has O(n^2) time complexity because of bitwise operations.
DTwo-pointer swap method has O(log n) time complexity as it swaps pairs.
Attempts:
2 left
💡 Hint
Consider how many times each element is accessed or swapped.

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