Rotate an Array to the Right
Updated
Problem statement
For this 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.
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 Onsite Interview questions
- Task Scheduler with DependenciesONSITE INTERVIEW · Seen Jul 2026
- Best Poker Category with a JokerPHONE SCREEN · ONSITE INTERVIEW · Seen Jun 2026
- Format and Validate a Model ResponseONSITE INTERVIEW · Seen Apr 2026
- Stateful Neuron Matrix UpdateONSITE INTERVIEW · Seen Apr 2026
- Clock Hand Angle with Subsecond PrecisionONSITE INTERVIEW · Seen Dec 2025
- Four-Player Matching Card GameONSITE INTERVIEW · Seen Feb 2025
- Rich-Text Segment Normalization and Bold ToggleONSITE INTERVIEW · Seen Apr 2024
- Minimum Distance Matching in a ForestPHONE SCREEN · ONSITE INTERVIEW · Seen Feb 2024
- Shortest Distance Between Two NodesONSITE INTERVIEW · Seen Jan 2024
- Fit Beta Parameters by Maximum LikelihoodONSITE INTERVIEW · Seen Sep 2022
Similar questions at other companies
- Merge Sorted ArrayDP World · ONSITE INTERVIEW
- Remove Duplicates From Sorted Array In PlaceKotak Mahindra Bank · PHONE SCREEN · ONSITE INTERVIEWKotak Mahindra Bank interview questions
- Maximize Distance to the Closest Occupied SeatAmazon · PHONE SCREEN · ONSITE INTERVIEW
- Merge Two Arrays Into Sorted OrderNVIDIA · ONSITE INTERVIEWNVIDIA interview questions
- Alternate Positive and Negative ValuesTesla · ONSITE INTERVIEWTesla interview questions
- Sparse Vector Dot Product from TuplesMeta · ONSITE INTERVIEWMeta interview questions
- Make a Sorted Array Unique In PlaceAlpaca · ONSITE INTERVIEW
- Two SumMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
- Maximize Distance to Closest Person — Return the SeatRoblox · ONSITE INTERVIEWRoblox interview questions
- Move ZeroesBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions