Rotate Array

Advanced

Rotate Array Challenge

Welcome to the Rotate Array Challenge! Today, we will make a fun function that helps you "rotate" an array. Imagine you have a line of toys, and you decide to move the first toy to the end of the line and shift every other toy one spot to the left. This is what we mean by rotating the array.

Your task is to create a function called rotate_array that takes an array of integers and a number of rotations. The array should be rotated to the left by the specified number of rotations. If you rotate an array of [1, 2, 3, 4, 5] two times, it will look like [3, 4, 5, 1, 2].

Examples

  • rotate_array([1, 2, 3, 4, 5], 2) should return [3, 4, 5, 1, 2]
  • rotate_array([10, 20, 30, 40, 50], 3) should return [40, 50, 10, 20, 30]
  • rotate_array([], 1) should return []

Note

  • If the array is empty, any number of rotations should still return an empty array.
  • Make sure to consider edge cases, like rotations larger than the length of the array!

Now let's get rotating!

Geek
Inputs

Arg 1:

[1,2,3,4,5]

Arg 2:

2
Expected Output
[3, 4, 5, 1, 2]
Actual Output
Rotate Array [advanced] | Coding for Kids