Bird
Raised Fist0
DSA Typescriptprogramming

Array Rotation Techniques in DSA Typescript

Choose your learning style10 modes available
Mental Model
Rotating an array means moving its elements so that some elements shift to the front or back, like a circle.
Analogy: Imagine a merry-go-round with seats numbered 1 to 5. Rotating the array is like spinning the merry-go-round so the seat numbers shift positions but stay in a circle.
Array: [1, 2, 3, 4, 5]
Rotation by 2 to the right:
Before: 1 -> 2 -> 3 -> 4 -> 5
After: 4 -> 5 -> 1 -> 2 -> 3
Dry Run Walkthrough
Input: array: [1, 2, 3, 4, 5], rotate right by 2
Goal: Move last 2 elements to the front, shifting others right
Step 1: Reverse the whole array
[5, 4, 3, 2, 1]
Why: Reversing prepares the array to reorder parts easily
Step 2: Reverse first 2 elements
[4, 5, 3, 2, 1]
Why: Fix the order of the rotated part at the front
Step 3: Reverse remaining elements from index 2 to end
[4, 5, 1, 2, 3]
Why: Fix the order of the rest of the array after rotation
Result:
[4, 5, 1, 2, 3]
Annotated Code
DSA Typescript
class ArrayRotator {
  static reverse(arr: number[], start: number, end: number): void {
    while (start < end) {
      const temp = arr[start];
      arr[start] = arr[end];
      arr[end] = temp;
      start++;
      end--;
    }
  }

  static rotateRight(arr: number[], k: number): void {
    const n = arr.length;
    if (n === 0) return; // handle empty array
    k = k % n; // handle rotation greater than length
    if (k === 0) return; // no rotation needed

    this.reverse(arr, 0, n - 1); // reverse whole array
    this.reverse(arr, 0, k - 1); // reverse first k elements
    this.reverse(arr, k, n - 1); // reverse rest
  }
}

// Driver code
const arr = [1, 2, 3, 4, 5];
ArrayRotator.rotateRight(arr, 2);
console.log(arr.join(', '));
k = k % n; // handle rotation greater than length
adjust rotation steps to array size to avoid extra cycles
if (k === 0) return; // no rotation needed
skip rotation if steps is zero after adjustment
this.reverse(arr, 0, n - 1); // reverse whole array
reverse entire array to prepare for rotation
this.reverse(arr, 0, k - 1); // reverse first k elements
reverse first part to fix rotated segment order
this.reverse(arr, k, n - 1); // reverse rest
reverse remaining part to fix order
OutputSuccess
4, 5, 1, 2, 3
Complexity Analysis
Time: O(n) because each element is reversed at most twice during the three reversals
Space: O(1) because rotation is done in place without extra arrays
vs Alternative: Naive rotation by shifting elements one by one is O(n*k), which is slower for large k; this method is efficient and fast
Edge Cases
rotation steps k is zero
array remains unchanged
DSA Typescript
if (k === 0) return; // no rotation needed
rotation steps k is multiple of array length
array remains unchanged because rotation cycles back
DSA Typescript
k = k % n; // handle rotation greater than length
empty array
no error, array remains empty
DSA Typescript
if (n === 0) return; // handle empty array
When to Use This Pattern
When you need to rotate or shift array elements efficiently without extra space, use the reverse method to reorder parts in place.
Common Mistakes
Mistake: Rotating by shifting elements one by one in a loop causing O(n*k) time
Fix: Use the three-step reverse method to achieve O(n) time
Mistake: Not handling rotation steps greater than array length
Fix: Use modulo operation k = k % n to normalize rotation steps
Mistake: Reversing parts in wrong order or wrong indices
Fix: Follow the exact sequence: reverse whole array, reverse first k elements, then reverse the rest
Summary
Rotates an array by moving elements around using reversals.
Use when you want to rotate array elements efficiently in place.
The key is reversing the whole array, then reversing parts to fix order.

Practice

(1/5)
1. What does rotating an array to the right by 1 position mean?
Example: [1, 2, 3, 4] rotated right by 1 becomes?
easy
A. [4, 1, 2, 3]
B. [2, 3, 4, 1]
C. [1, 2, 3, 4]
D. [3, 4, 1, 2]

Solution

  1. Step 1: Understand right rotation

    Right rotation by 1 means the last element moves to the front, shifting others right.
  2. Step 2: Apply to example array

    Last element 4 moves to front, rest shift right: [4, 1, 2, 3].
  3. Final Answer:

    [4, 1, 2, 3] -> Option A
  4. Quick Check:

    Right rotate by 1 = last element front [OK]
Hint: Right rotate by 1 means last element moves front [OK]
Common Mistakes:
  • Confusing right rotation with left rotation
  • Not moving the last element to front
  • Shifting elements incorrectly
2. Which of the following TypeScript code snippets correctly rotates an array arr to the left by 1 position?
easy
A. arr.push(arr.shift()!)
B. arr.unshift(arr.pop()!)
C. arr.pop(); arr.unshift(arr.pop()!)
D. arr.shift(); arr.push(arr.shift()!)

Solution

  1. Step 1: Understand left rotation by 1

    Left rotation by 1 means first element moves to the end.
  2. Step 2: Check code snippets

    arr.push(arr.shift()!) uses arr.shift() to remove first element and arr.push() to add it at end, correct for left rotation.
  3. Final Answer:

    arr.push(arr.shift()!) -> Option A
  4. Quick Check:

    Left rotate = shift then push [OK]
Hint: Left rotate by 1 = remove first, add to end [OK]
Common Mistakes:
  • Using pop instead of shift for left rotation
  • Using unshift with pop for left rotation
  • Calling shift or pop twice incorrectly
3. What is the output of the following TypeScript code?
const arr = [10, 20, 30, 40, 50];
const k = 2;
const rotated = arr.slice(-k).concat(arr.slice(0, arr.length - k));
console.log(rotated);
medium
A. [50, 10, 20, 30, 40]
B. [40, 50, 10, 20, 30]
C. [20, 30, 40, 50, 10]
D. [30, 40, 50, 10, 20]

Solution

  1. Step 1: Understand slicing for rotation

    arr.slice(-k) takes last 2 elements: [40, 50]. arr.slice(0, arr.length - k) takes first 3 elements: [10, 20, 30].
  2. Step 2: Concatenate slices

    Concatenating gives [40, 50, 10, 20, 30], which is right rotation by 2.
  3. Final Answer:

    [40, 50, 10, 20, 30] -> Option B
  4. Quick Check:

    Right rotate by 2 = last 2 front [OK]
Hint: Right rotate by k = last k elements + rest [OK]
Common Mistakes:
  • Using slice with wrong indices
  • Confusing left and right rotation
  • Not concatenating slices in correct order
4. Find the error in this TypeScript function that rotates an array to the left by k positions:
function rotateLeft(arr: number[], k: number): number[] {
  for (let i = 0; i < k; i++) {
    arr.push(arr.shift());
  }
  return arr;
}
medium
A. arr.push() should be arr.unshift()
B. Loop should run from i = 1 to k
C. arr.shift() can return undefined, causing push(undefined)
D. Function should return void, not number[]

Solution

  1. Step 1: Check arr.shift() return type

    arr.shift() returns element or undefined if array empty. Here, no check for undefined.
  2. Step 2: Effect on arr.push()

    If arr.shift() returns undefined, arr.push(undefined) adds undefined to array, causing incorrect result or runtime issues.
  3. Final Answer:

    arr.shift() can return undefined, causing push(undefined) -> Option C
  4. Quick Check:

    shift may be undefined, must check [OK]
Hint: Check shift() return before push to avoid undefined [OK]
Common Mistakes:
  • Ignoring undefined from shift()
  • Wrong loop range assumptions
  • Confusing push and unshift methods
5. Given an array arr of length n, how can you rotate it to the left by k positions efficiently without using extra space? Choose the correct approach.
hard
A. Use a temporary array to store first k elements, then shift rest left, then copy temp at end
B. Sort the array and then rotate by k positions
C. Rotate one by one k times using shift and push
D. Reverse first k elements, reverse last n-k elements, then reverse whole array

Solution

  1. Step 1: Understand in-place rotation using reversal

    Reversing parts of array and then whole array rotates left by k without extra space.
  2. Step 2: Apply reversal steps

    Reverse first k elements, reverse last n-k elements, then reverse entire array to get rotated array.
  3. Final Answer:

    Reverse first k, reverse last n-k, reverse whole array -> Option D
  4. Quick Check:

    Reversal method = in-place left rotate [OK]
Hint: Use triple reversal for in-place left rotation [OK]
Common Mistakes:
  • Using extra array when not needed
  • Rotating one by one causing O(n*k) time
  • Sorting array unrelated to rotation
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard