Challenge - 5 Problems
Array Reversal Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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; }
Attempts:
2 left
💡 Hint
Think about how swapping elements from start and end moves towards the center.
✗ Incorrect
The code swaps elements from the start and end moving inward, effectively reversing the array.
After reversal, the array elements print as: 5 4 3 2 1
❓ Predict Output
intermediate2: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; }
Attempts:
2 left
💡 Hint
Recursion swaps outer elements and moves inward.
✗ Incorrect
The recursive function swaps the first and last elements, then calls itself for the inner subarray.
Resulting reversed array: 40 30 20 10
🔧 Debug
advanced2: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; }
Attempts:
2 left
💡 Hint
Check the index used for swapping elements carefully.
✗ Incorrect
The code accesses arr[n - i], but valid indices go up to n-1.
When i=0, arr[n - 0] = arr[4] is out of bounds causing runtime error.
❓ Predict Output
advanced2: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; }
Attempts:
2 left
💡 Hint
XOR swap exchanges values without a temporary variable.
✗ Incorrect
The XOR swap swaps elements at start and end indices without extra space.
After reversal, array prints as: 9 8 7
🧠 Conceptual
expert2: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)?
Attempts:
2 left
💡 Hint
Consider how many times each element is accessed or swapped.
✗ Incorrect
All three methods visit each element once or swap pairs once, resulting in linear time O(n).
Recursion and XOR swap do not increase complexity beyond O(n).
