Challenge - 5 Problems
Array Reversal Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateWhat is the output of this array reversal code?
Consider the following JavaScript code that reverses an array. What will be printed?
DSA Javascript
const arr = [1, 2, 3, 4, 5]; for(let i = 0; i < arr.length / 2; i++) { const temp = arr[i]; arr[i] = arr[arr.length - 1 - i]; arr[arr.length - 1 - i] = temp; } console.log(arr);
Attempts:
2 left
💡 Hint
Think about swapping elements from start and end moving towards the center.
✗ Incorrect
The code swaps elements from the start and end of the array moving towards the center, effectively reversing the array in place.
❓ Predict Output
intermediateWhat is the output of this code using built-in reverse?
What will be the output of this JavaScript code snippet?
DSA Javascript
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 array. So both reversed and arr refer to the reversed array.
❓ Predict Output
advancedWhat is the output of this recursive array reversal function?
Analyze the following recursive function that reverses an array. What will be printed?
DSA Javascript
function reverseArray(arr) {
if (arr.length === 0) return [];
return [arr.pop()].concat(reverseArray(arr));
}
const original = [1, 2, 3];
const reversed = reverseArray(original.slice());
console.log(reversed);Attempts:
2 left
💡 Hint
The function removes the last element and adds it to the front recursively.
✗ Incorrect
The function pops the last element and concatenates it with the reversed rest recursively, producing a reversed array.
❓ Predict Output
advancedWhat is the output of this code reversing a linked list represented as an array of nodes?
Given the array representing a singly linked list nodes, what will be the output after reversal?
DSA Javascript
const nodes = [
{value: 1, next: 1},
{value: 2, next: 2},
{value: 3, next: null}
];
function reverseList(nodes) {
let prev = null;
let currentIndex = 0;
while (currentIndex !== null) {
const currentNode = nodes[currentIndex];
const nextIndex = currentNode.next;
currentNode.next = prev;
prev = currentIndex;
currentIndex = nextIndex;
}
return prev;
}
const newHead = reverseList(nodes);
const result = [];
let idx = newHead;
while (idx !== null) {
result.push(nodes[idx].value);
idx = nodes[idx].next;
}
console.log(result);Attempts:
2 left
💡 Hint
The code reverses the linked list by changing next pointers.
✗ Incorrect
The while loop reverses the next pointers of each node, so the list order becomes 3 -> 2 -> 1.
🧠 Conceptual
expertWhich statement about array reversal time complexity is correct?
Consider reversing an array of length n. Which statement below correctly describes the time complexity of reversing the array in place?
Attempts:
2 left
💡 Hint
Think about how many swaps happen when reversing an array.
✗ Incorrect
Each element is swapped once with its counterpart, so the total operations scale linearly with n.
