Rotate Array by K
Updated
Problem statement
You are given an integer array nums and a nonnegative integer k. Rotate the array k positions to the right and return the result. Each step moves every value one place right, and the last value wraps around to the front. If the array is empty, return an empty array.
Every value must stay in the result, and the values keep their order around the circle. You may build a new array. You do not have to change nums in place.
Function
rotateArray(nums: int[], k: int) → int[]Examples
Example 1
nums = [1,2,3,4,5]k = 2return = [4,5,1,2,3]Each step moves the last value to the front. After two steps, the last two values sit at the front, followed by the rest in their old order.
Example 2
nums = [-1,7,0]k = 4return = [0,-1,7]The array has three values, so every three steps bring it back to where it started. Four steps are the same as one step, which moves the last value to the front.
Constraints
0 <= nums.length <= 10^5.-10^9 <= nums[i] <= 10^9.0 <= k <= 10^9.
More Apple Phone Screen questions
- LRU CachePHONE SCREEN · ONSITE INTERVIEW · Seen Dec 2026
- Topological OrderingPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Merge IntervalsOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Subarray Product Less Than KPHONE SCREEN · Seen Nov 2026
- Evaluate DivisionPHONE SCREEN · Seen Nov 2026
- Reverse a Linked ListPHONE SCREEN · Seen Nov 2026
- Validate a BST with a Custom ComparisonPHONE SCREEN · Seen Nov 2026
- Design a Food Rating SystemPHONE SCREEN · Seen Oct 2026
- Days Until Poisoned Plants StabilizePHONE SCREEN · Seen Oct 2026
- Capture Surrounded Regions with Union-FindPHONE SCREEN · Seen Oct 2026
Similar questions at other companies
- Numerically Stable SoftmaxWaymo · PHONE SCREENWaymo interview questions
- Streaming Entropy, Part 1: Batch EntropyOpenAI · PHONE SCREEN · ONSITE INTERVIEWOpenAI interview questions
- Merchant Payout Reserve CalculatorStripe · PHONE SCREENStripe interview questions
- Automation Pipeline, Part 1: Single WorkerPlaid · PHONE SCREENPlaid interview questions
- Employee Count Median, Mean and ModeClay · PHONE SCREEN
- Length of a Document Reached Through a Random TokenMercor · PHONE SCREENMercor interview questions
- Apply Affine Array TransformationsByteDance · PHONE SCREENByteDance interview questions
- Exponential Backoff DelaysWonderful · PHONE SCREEN
- Recover Document Hierarchy from IndentationReducto · PHONE SCREEN
- Average Grades per StudentHartford Financial Services · PHONE SCREENHartford Financial Services interview questions