Click4Ai

12.

Easy

Write a function rotate_array(arr, k) that rotates an array to the right by k positions. Elements that go past the end wrap around to the beginning.

Example:

Input: arr = [1, 2, 3, 4, 5], k = 2

Output: [4, 5, 1, 2, 3]

Input: arr = [1, 2, 3], k = 1

Output: [3, 1, 2]

Constraints:

  • Array length: 1 <= len(arr) <= 1000
  • k >= 0 (can be larger than array length)
  • Test Cases

    Test Case 1
    Input: [1, 2, 3, 4, 5], 2
    Expected: [4, 5, 1, 2, 3]
    Test Case 2
    Input: [1, 2, 3], 1
    Expected: [3, 1, 2]
    Test Case 3
    Input: [1, 2, 3, 4], 0
    Expected: [1, 2, 3, 4]
    + 2 hidden test cases