FastPrepRotate Array

Rotate Array

Microsoft logoMicrosoft● MediumFULLTIMEONSITE INTERVIEW
Learn

Problem statement

Given an integer array nums and a nonnegative integer k, rotate the array to the right by k positions.

Perform the rotation in place. For the practice runner, return the same array after it has been mutated.

Function

rotate(nums: int[], k: int) → int[]

Examples

Example 1

nums = [1,2,3,4,5,6,7]k = 3return = [5,6,7,1,2,3,4]

The last three values move to the front while their relative order is preserved.

Example 2

nums = [-1,-100,3,99]k = 2return = [3,99,-1,-100]

After two right rotations, 3 and 99 precede the original first two values.

Constraints

  • 1 <= nums.length <= 100000
  • -10^9 <= nums[i] <= 10^9
  • 0 <= k <= 10^9

More Microsoft problems

See Microsoft hiring insights
public int[] rotate(int[] nums, int k) {
  // Write your code here.
}
nums[1,2,3,4,5,6,7]
k3
expected[5,6,7,1,2,3,4]
Checking account…