Challenge - 5 Problems
Array Reversal Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Array Reversal Using Two Pointers
What is the output of the following Java code that reverses an array using two pointers?
DSA Java
public class Main { public static void main(String[] args) { int[] arr = {1, 2, 3, 4, 5}; int left = 0, right = arr.length - 1; while (left < right) { int temp = arr[left]; arr[left] = arr[right]; arr[right] = temp; left++; right--; } for (int num : arr) { System.out.print(num + " "); } } }
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.
❓ Predict Output
intermediateOutput of Array Reversal Using Recursion
What is the output of this Java code that reverses an array recursively?
DSA Java
public class Main { public static 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); } public static void main(String[] args) { int[] arr = {10, 20, 30, 40}; reverse(arr, 0, arr.length - 1); for (int num : arr) { System.out.print(num + " "); } } }
Attempts:
2 left
💡 Hint
Recursion swaps outer elements and moves inward.
✗ Incorrect
The recursive function swaps elements at start and end indices until they meet or cross, reversing the array.
🔧 Debug
advancedIdentify the Error in Array Reversal Code
What error will this Java code produce when trying to reverse an array?
DSA Java
public class Main { public static void main(String[] args) { int[] arr = {1, 2, 3, 4}; for (int i = 0; i <= arr.length / 2; i++) { int temp = arr[i]; arr[i] = arr[arr.length - i - 1]; arr[arr.length - i - 1] = temp; } for (int num : arr) { System.out.print(num + " "); } } }
Attempts:
2 left
💡 Hint
Check the index used for swapping elements.
✗ Incorrect
The code accesses arr[arr.length - i], which is out of bounds since array indices go from 0 to length-1.
🧠 Conceptual
advancedTime Complexity of Array Reversal
What is the time complexity of reversing an array of size n using the two-pointer swapping method?
Attempts:
2 left
💡 Hint
Consider how many swaps are done relative to the array size.
✗ Incorrect
Each element is swapped at most once, so the operation scales linearly with n.
❓ Predict Output
expertOutput of Reversing Array Using Streams (Java 8+)
What is the output of this Java code that reverses an array using streams?
DSA Java
import java.util.Arrays; public class Main { public static void main(String[] args) { int[] arr = {3, 6, 9, 12}; int[] reversed = Arrays.stream(arr) .boxed() .sorted((a, b) -> b - a) .mapToInt(Integer::intValue) .toArray(); for (int num : reversed) { System.out.print(num + " "); } } }
Attempts:
2 left
💡 Hint
The sorted comparator reverses the order.
✗ Incorrect
The stream boxes ints to Integers, sorts them in descending order, then converts back to int array.
