Challenge - 5 Problems
Array Reversal Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of In-Place Array Reversal
What is the output of the following TypeScript code that reverses an array in place?
DSA Typescript
function reverseArray(arr: number[]): void {
let left = 0;
let right = arr.length - 1;
while (left < right) {
const temp = arr[left];
arr[left] = arr[right];
arr[right] = temp;
left++;
right--;
}
}
const nums = [1, 2, 3, 4, 5];
reverseArray(nums);
console.log(nums);Attempts:
2 left
💡 Hint
Think about swapping elements from the start and end moving towards the center.
✗ Incorrect
The function swaps elements from the start and end of the array moving inward, reversing the array in place.
❓ Predict Output
intermediateOutput of Array Reverse Using Built-in Method
What will be printed after running this TypeScript code that uses the built-in reverse method?
DSA Typescript
const arr = ['a', 'b', 'c', 'd']; const reversed = arr.reverse(); console.log(reversed); console.log(arr);
Attempts:
2 left
💡 Hint
Remember that reverse() modifies the original array and returns it.
✗ Incorrect
The reverse() method reverses the array in place and returns the same reversed array, so both variables show the reversed array.
🔧 Debug
advancedIdentify the Error in Recursive Array Reversal
What error will this TypeScript code produce when trying to reverse an array recursively?
DSA Typescript
function recursiveReverse(arr: number[], start: number, end: number): void {
if (start >= end) return;
const temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
recursiveReverse(arr, start + 1, end - 1);
}
const numbers = [10, 20, 30, 40];
recursiveReverse(numbers, 0, numbers.length - 1);
console.log(numbers);Attempts:
2 left
💡 Hint
Trace the recursive calls step by step.
✗ Incorrect
No error occurs. The code correctly reverses the array to [40, 30, 20, 10]. The base case start >= end terminates recursion properly after the pointers cross or meet.
❓ Predict Output
advancedOutput of Array Reversal Using Spread and Reduce
What is the output of this TypeScript code that reverses an array using spread operator and reduce?
DSA Typescript
const original = [1, 2, 3, 4]; const reversed = [...original].reduce((acc, val) => [val, ...acc], []); console.log(reversed);
Attempts:
2 left
💡 Hint
The reduce function builds a new array by adding current value at the front.
✗ Incorrect
The reduce starts with empty array and prepends each element, effectively reversing the array.
🧠 Conceptual
expertTime Complexity of Different Array Reversal Techniques
Which statement correctly describes the time complexity of reversing an array of size n using an in-place swap method versus creating a new reversed array?
Attempts:
2 left
💡 Hint
Consider how many times each element is accessed or moved.
✗ Incorrect
Both methods visit each element once, so both run in linear time O(n).
