Challenge - 5 Problems
Array Reversal Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput 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] << " "; }
Attempts:
2 left
💡 Hint
Think about swapping elements from the start and end moving towards the center.
✗ Incorrect
The code swaps the first and last elements, then moves inward, effectively reversing the array.
❓ Predict Output
intermediateOutput 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] << " "; } }
Attempts:
2 left
💡 Hint
The function swaps outer elements and calls itself for inner elements.
✗ Incorrect
The recursion swaps the first and last elements, then moves inward until the base case.
🧠 Conceptual
advancedTime 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?
Attempts:
2 left
💡 Hint
Consider how many swaps happen relative to the array size.
✗ Incorrect
Each element is swapped at most once, so the operation is linear in the size of the array.
🔧 Debug
advancedIdentify 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] << " "; }
Attempts:
2 left
💡 Hint
Check the value of 'end' and array indexing.
✗ Incorrect
The variable 'end' is initialized to n, which is outside the array index range (0 to n-1), causing out-of-bounds access.
🚀 Application
expertReversing 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
Attempts:
2 left
💡 Hint
Focus on swapping elements only within the subarray boundaries.
✗ Incorrect
Option B correctly swaps elements within the subarray by offsetting indices properly. Option B modifies start and end variables directly, which would affect the original indices outside the loop. Option B swaps incorrectly and causes double swaps. Option B reverses from the start of the array, not the subarray.
