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 a for loop
What is the output of the following C# code that reverses an array using a for loop?
DSA C#
int[] arr = {1, 2, 3, 4, 5}; int n = arr.Length; for (int i = 0; i < n / 2; i++) { int temp = arr[i]; arr[i] = arr[n - i - 1]; arr[n - i - 1] = temp; } foreach (int num in arr) { Console.Write(num + " "); }
Attempts:
2 left
💡 Hint
Think about swapping elements from start and end moving towards the center.
✗ Incorrect
The for loop swaps elements from the start and end of the array until the middle is reached, effectively reversing the array.
❓ Predict Output
intermediateOutput of reversing an array using Array.Reverse method
What is the output of the following C# code that uses Array.Reverse to reverse an array?
DSA C#
int[] arr = {10, 20, 30, 40}; Array.Reverse(arr); foreach (int num in arr) { Console.Write(num + " "); }
Attempts:
2 left
💡 Hint
Array.Reverse reverses the entire array in place.
✗ Incorrect
Array.Reverse reverses the elements of the array in place, so the output is the reversed array.
🔧 Debug
advancedIdentify the error in this array reversal code
What error will this C# code produce when trying to reverse an array?
DSA C#
int[] arr = {1, 2, 3, 4}; int n = arr.Length; for (int i = 0; i <= n / 2; i++) { int temp = arr[i]; arr[i] = arr[n - i - 1]; arr[n - i - 1] = temp; } foreach (int num in arr) { Console.Write(num + " "); }
Attempts:
2 left
💡 Hint
Check the loop condition and array indexing carefully.
✗ Incorrect
The loop runs one step too far causing an index out of range when accessing arr[n - i - 1] at i = n/2.
🧠 Conceptual
advancedTime complexity of array reversal
What is the time complexity of reversing an array of size n using the standard two-pointer swap method?
Attempts:
2 left
💡 Hint
Consider how many swaps are done relative to the array size.
✗ Incorrect
Each element is swapped once, so the number of operations grows linearly with n.
🚀 Application
expertOutput after reversing a subarray
Given the array arr = {5, 10, 15, 20, 25, 30} and the code below that reverses a subarray from index 1 to 4, what is the final array?
DSA C#
int[] arr = {5, 10, 15, 20, 25, 30}; int start = 1, end = 4; while (start < end) { int temp = arr[start]; arr[start] = arr[end]; arr[end] = temp; start++; end--; } foreach (int num in arr) { Console.Write(num + " "); }
Attempts:
2 left
💡 Hint
Only the elements between index 1 and 4 are reversed.
✗ Incorrect
The subarray {10, 15, 20, 25} is reversed to {25, 20, 15, 10}, rest remain same.
