5. Rotate Array

5. Rotate Array

Easy
54.0%
1.2k
arraymathtwo-pointers
Google, Amazon, Netflix, Meta (Facebook), Apple, Microsoft, Uber, Bloomberg, Airbnb, Stripe, Adobe, Salesforce, LinkedIn, Oracle

You’re interviewing for a backend/data engineering role at a FAANG-level company. The interviewer describes a real product scenario:

A large-scale content platform displays trending items in a rotating banner. Every few minutes, the platform shifts the items to the right so that newer or more relevant content appears first, while older content moves toward the end.

The items are stored in an integer array nums, and the platform performs a right rotation by k steps.

Due to performance constraints:

  • The rotation must be done efficiently
  • The operation should modify the array in place
  • The system should handle very large arrays smoothly

This is a common coding interview / DSA question might ask in Google, Amazon, Netflix, Meta, Apple, Microsoft, Uber, and Bloomberg interviews.

Your Task

Given an integer array nums and a non-negative integer k, rotate the array to the right by k steps.

Input Format

  • nums: an array of integers
  • k: a non-negative integer representing the number of rotation steps

Output Format

  • Modify the array nums in place so that it is rotated to the right by k steps
  • Return the rotated array

Examples

Example 1:

Input: nums = [1, 2, 3, 4, 5, 6, 7] k = 3
Output: [5, 6, 7, 1, 2, 3, 4]

Example 2:

Input: nums = [-1, -100, 3, 99] k = 2
Output: [3, 99, -1, -100]

Constraints

  • 1 <= nums.length <= 100,000
  • -10^9 <= nums[i] <= 10^9
  • 0 <= k <= 100,000
  • k may be greater than the length of the array

Loading editor...

[1,2,3,4,5,6,7], 3
[5,6,7,1,2,3,4]

Console Output

Click "Run Code" or "Submit" to see results

Your code will be evaluated by AI with instant feedback