189. Rotate Array
MediumSolutions in Python, Java, C++, TypeScript
Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.
Approach
The core task is to rotate an array nums to the right by k steps. A right rotation by k steps means the last k elements of the array move to the front, and the first n-k elements shift to the back. The main challenge is to perform this operation in-place, meaning without using significant extra memory, ideally with O(1) space complexity.
Solution
Rotate Array in Python
class Solution:
def rotate(self, nums: List[int], k: int) -> None:
"""
Do not return anything, modify nums in-place instead.
"""
n = len(nums)
# Handle edge cases: empty array or no rotation
if n == 0:
return
# Normalize k to be within the bounds of the array length
k %= n
if k == 0:
return
# Helper function to reverse a sub-array in-place
def reverse(start: int, end: int):
while start < end:
# Swap elements at start and end pointers
nums[start], nums[end] = nums[end], nums[start]
start += 1
end -= 1
# Step 1: Reverse the entire list
# Example: [1,2,3,4,5,6,7], k=3 -> [7,6,5,4,3,2,1]
reverse(0, n - 1)
# Step 2: Reverse the first k elements
# Example: [7,6,5,4,3,2,1] -> [5,6,7,4,3,2,1]
reverse(0, k - 1)
# Step 3: Reverse the remaining n-k elements
# Example: [5,6,7,4,3,2,1] -> [5,6,7,1,2,3,4]
reverse(k, n - 1)
Rotate Array in Java
class Solution {
public void rotate(int[] nums, int k) {
int n = nums.length;
// Handle edge cases: empty array or no rotation
if (n == 0) {
return;
}
// Normalize k to be within the bounds of the array length
k %= n;
if (k == 0) {
return;
}
// Step 1: Reverse the entire array
// Example: [1,2,3,4,5,6,7], k=3 -> [7,6,5,4,3,2,1]
reverse(nums, 0, n - 1);
// Step 2: Reverse the first k elements
// Example: [7,6,5,4,3,2,1] -> [5,6,7,4,3,2,1]
reverse(nums, 0, k - 1);
// Step 3: Reverse the remaining n-k elements
// Example: [5,6,7,4,3,2,1] -> [5,6,7,1,2,3,4]
reverse(nums, k, n - 1);
}
// Helper function to reverse a sub-array in-place
private void reverse(int[] nums, int start, int end) {
while (start < end) {
// Swap elements at start and end pointers
int temp = nums[start];
nums[start] = nums[end];
nums[end] = temp;
start++;
end--;
}
}
}
Rotate Array in C++
class Solution {
public:
void rotate(vector<int>& nums, int k) {
int n = nums.size();
// Handle edge cases: empty array or no rotation
if (n == 0) {
return;
}
// Normalize k to be within the bounds of the array length
k = k % n;
if (k == 0) {
return;
}
// Step 1: Reverse the entire array
// Example: [1,2,3,4,5,6,7], k=3 -> [7,6,5,4,3,2,1]
reverse(nums, 0, n - 1);
// Step 2: Reverse the first k elements
// Example: [7,6,5,4,3,2,1] -> [5,6,7,4,3,2,1]
reverse(nums, 0, k - 1);
// Step 3: Reverse the remaining n-k elements
// Example: [5,6,7,4,3,2,1] -> [5,6,7,1,2,3,4]
reverse(nums, k, n - 1);
}
private:
// Helper function to reverse a sub-array in-place
void reverse(vector<int>& nums, int start, int end) {
while (start < end) {
// Swap elements at start and end pointers
swap(nums[start], nums[end]);
start++;
end--;
}
}
};
Rotate Array in TypeScript
/**
Do not return anything, modify nums in-place instead.
*/
function rotate(nums: number[], k: number): void {
const n = nums.length;
// Handle edge cases: empty array or no rotation
if (n === 0) {
return;
}
// Normalize k to be within the bounds of the array length
k %= n;
if (k === 0) {
return;
}
// Helper function to reverse a sub-array in-place
const reverse = (start: number, end: number): void => {
while (start < end) {
// Swap elements at start and end pointers using destructuring assignment
[nums[start], nums[end]] = [nums[end], nums[start]];
start++;
end--;
}
};
// Step 1: Reverse the entire array
// Example: [1,2,3,4,5,6,7], k=3 -> [7,6,5,4,3,2,1]
reverse(0, n - 1);
// Step 2: Reverse the first k elements
// Example: [7,6,5,4,3,2,1] -> [5,6,7,4,3,2,1]
reverse(0, k - 1);
// Step 3: Reverse the remaining n-k elements
// Example: [5,6,7,4,3,2,1] -> [5,6,7,1,2,3,4]
reverse(k, n - 1);
};
More Medium LeetCode answers
Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.
LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.