FastPrepRotate an Array to the Right

Rotate an Array to the Right

Scale AI logoScale AI● EasyNEW GRADONSITE INTERVIEW
Learn

Problem statement

For this practice exercise, you are given an integer array nums and a nonnegative integer k. Return the array after rotating its elements to the right by k steps.

One right step moves the final element to the front and shifts every other element one position to the right. Preserve element values, duplicate counts and cyclic order.

A complete cycle leaves the order unchanged. If nums is empty, return an empty array. You may modify nums or build a separate result array.

Interview context

The onsite report names an array-rotation exercise, says the implementation must compile, and emphasizes corner cases. It does not preserve the direction, step count, element type, output interface or an in-place requirement. Rightward rotation by an explicit k and the integer-array return contract above are practice choices.

Function

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

Examples

Example 1

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

The last two values move to the front; the original order is preserved within both parts.

Example 2

nums = [7,7,-2,0]k = 6return = [-2,0,7,7]

Six steps on four elements equal two steps. Duplicates and negative values keep their relative order.

Example 3

nums = []k = 10return = []

An empty input returns an empty array before taking a remainder by its length.

Constraints

  • For this exercise, assume 0 <= nums.length <= 100000.
  • For this exercise, assume 0 <= k <= 1000000000.
  • For this exercise, assume every element is an integer in [-1000000000,1000000000]; duplicates are allowed.

More Scale AI problems

See Scale AI hiring insights
public int[] rotateArrayRight(int[] nums, int k) {
    // write your code here
}
nums[1,2,3,4,5]
k2
expected[4,5,1,2,3]
Checking account…