Apply a Sequence of Array Rotations
Updated
Problem statement
You are given an integer array nums and an integer array rotations. Apply the rotations to nums one after another, in order:
- a positive value
rrotates the array right byrsteps (each step moves the last element to the front); - a negative value
rrotates the array left by-rsteps (each step moves the first element to the end); 0leaves the array unchanged.
Return the array after all the rotations.
Function
applyRotations(nums: int[], rotations: int[]) → int[]Examples
Example 1
nums = [1,2,3,4,5]rotations = [2]return = [4,5,1,2,3]Rotating right by 2 moves the last two elements to the front: [4, 5, 1, 2, 3].
Example 2
nums = [1,2,3,4,5]rotations = [1,-3,7]return = [1,2,3,4,5]Right by 1 gives [5, 1, 2, 3, 4]. Left by 3 gives [3, 4, 5, 1, 2]. Right by 7 is the same as right by 2, because 5 steps bring the array back to where it was, so it gives [1, 2, 3, 4, 5]. In total the array moved 1 - 3 + 7 = 5 steps right, one full turn.
Example 3
nums = [10,20,30]rotations = [-1,-1]return = [30,10,20]Left by 1 gives [20, 30, 10], and left by 1 again gives [30, 10, 20].
Constraints
1 <= nums.length <= 2000-10^9 <= nums[i] <= 10^91 <= rotations.length <= 10^4-10^9 <= rotations[j] <= 10^9
More Microsoft Onsite Interview questions
- Longest Substring Without Repeating CharactersONSITE INTERVIEW · Seen Nov 2026
- Valid ParenthesesONSITE INTERVIEW · Seen Nov 2026
- Count a User's Actions in an Event LogONSITE INTERVIEW · Seen Nov 2026
- Smallest Batch Size Within a Step LimitONSITE INTERVIEW · Seen Nov 2026
- Count Subarrays with Equal Even and Odd CountsONSITE INTERVIEW · Seen Nov 2026
- Minimum Time to Rot All OrangesONSITE INTERVIEW · Seen Nov 2026
- Coin ChangeONSITE INTERVIEW · Seen Oct 2026
- Daily USD Totals with Effective-Dated Exchange RatesONSITE INTERVIEW · Seen Oct 2026
- Number Words to Digits in Any LanguageONSITE INTERVIEW · Seen Oct 2026
- Totals from a Tiered Rate TableONSITE INTERVIEW · Seen Oct 2026
Similar questions at other companies
- Expected Value of Modified DiceJane Street · ONSITE INTERVIEWJane Street interview questions
- Reservoir Sampling with Recorded DrawsMeta · ONSITE INTERVIEWMeta interview questions
- Group-Relative Policy AdvantagesAmazon · ONSITE INTERVIEW
- Reservoir Sampling with Recorded DrawsStackAdapt · ONSITE INTERVIEWStackAdapt interview questions
- Sliding-Window Averages as Reduced FractionsCoinbase · ONSITE INTERVIEWCoinbase interview questions
- Generate Unique Random NumbersGoogle · ONSITE INTERVIEWGoogle interview questions
- K-Means Delivery ZonesDoorDash · PHONE SCREEN · ONSITE INTERVIEWDoorDash interview questions
- Moving Cost by Volume and CategoryWayfair · ONSITE INTERVIEWWayfair interview questions
- Tensor-Parallel MLP GradientsOpenAI · ONSITE INTERVIEWOpenAI interview questions
- Vectorized K-Means AssignmentsSambaNova Systems · ONSITE INTERVIEWSambaNova Systems interview questions