Rotate a list to the right by k steps
Coding problemEasy-MediumAsked in: Adobe, Amazon, Apple, Google, Meta, Microsoft
Given a list of integers and a number k, rotate the list to the right by k steps and return it. One step moves the last value to the front.
k can be much larger than the length of the list, and the list can be empty. The largest hidden tests have 300,000 values, so moving one step at a time is too slow.
Examples
Example 1
Input: nums = [1,2,3,4,5,6,7], k = 3
Output: [5,6,7,1,2,3,4]
Example 2
Input: nums = [1,2,3], k = 4
Output: [3,1,2]
Rotating 3 steps gives the same list back, so 4 steps is the same as 1.
Example 3
Input: nums = [], k = 5
Output: []
Constraints
- The list holds between 0 and 300,000 integers
- k is between 0 and 1,000,000,000
Run checks the 3 examples. Submit also runs the hidden tests: edge cases, large inputs and a time limit.
Run your code to see the results here. Ctrl or Cmd + Enter runs; add Shift to submit.