Challenge - 5 Problems
Array Rotation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Left Rotation by 2
What is the output of the following TypeScript code after rotating the array to the left by 2 positions?
DSA Typescript
function leftRotate(arr: number[], d: number): number[] {
const n = arr.length;
const rotated = arr.slice(d % n).concat(arr.slice(0, d % n));
return rotated;
}
const arr = [10, 20, 30, 40, 50];
const result = leftRotate(arr, 2);
console.log(result);Attempts:
2 left
💡 Hint
Think about moving the first two elements to the end of the array.
✗ Incorrect
Left rotation by 2 means the first two elements (10, 20) move to the end, so the array becomes [30, 40, 50, 10, 20].
❓ Predict Output
intermediateOutput of Right Rotation by 3
What is the output of the following TypeScript code after rotating the array to the right by 3 positions?
DSA Typescript
function rightRotate(arr: number[], d: number): number[] {
const n = arr.length;
const rotated = arr.slice(n - (d % n)).concat(arr.slice(0, n - (d % n)));
return rotated;
}
const arr = [1, 2, 3, 4, 5, 6];
const result = rightRotate(arr, 3);
console.log(result);Attempts:
2 left
💡 Hint
Right rotation by 3 means last 3 elements move to the front.
✗ Incorrect
Right rotation by 3 moves elements 4,5,6 to the front, resulting in [4,5,6,1,2,3].
🔧 Debug
advancedIdentify the Error in Rotation Code
The following TypeScript function is intended to rotate an array to the left by d positions. What error will it cause when run?
DSA Typescript
function rotateLeft(arr: number[], d: number): number[] {
const n = arr.length;
return arr.slice(d).concat(arr.slice(0, d));
}
const result = rotateLeft([], 3);
console.log(result);Attempts:
2 left
💡 Hint
What happens when you slice an empty array?
✗ Incorrect
Slicing an empty array returns an empty array, so concatenating two empty arrays results in an empty array with no error.
🧠 Conceptual
advancedMinimum Rotations to Restore Original Array
Given an array of length 7, what is the minimum number of left rotations needed to restore the array to its original order?
Attempts:
2 left
💡 Hint
Think about how many rotations bring the array back to start.
✗ Incorrect
Rotating an array by its length (7) returns it to the original order.
🚀 Application
expertResult of Multiple Rotations
Starting with array [1, 2, 3, 4, 5], you perform a left rotation by 2, then a right rotation by 3. What is the final array?
DSA Typescript
function leftRotate(arr: number[], d: number): number[] {
const n = arr.length;
return arr.slice(d % n).concat(arr.slice(0, d % n));
}
function rightRotate(arr: number[], d: number): number[] {
const n = arr.length;
return arr.slice(n - (d % n)).concat(arr.slice(0, n - (d % n)));
}
let arr = [1, 2, 3, 4, 5];
arr = leftRotate(arr, 2);
arr = rightRotate(arr, 3);
console.log(arr);Attempts:
2 left
💡 Hint
Perform each rotation step by step and track the array.
✗ Incorrect
Left rotation by 2: [3,4,5,1,2]; then right rotate by 3: [5,1,2,3,4].
