Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Rotate Array Right

EasyPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Rotate Array Right. Start with the requirements on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

SDG&E's outage-monitoring pipeline stores ordered telemetry readings in an integer array. Given the readings and a nonnegative integer k, rotate the array to the right by k positions in place, preserving the relative order of the moved elements.

Formal Specification

Implement rotate(nums, k), where nums is a mutable list of integers and k is the number of right rotations. Modify nums directly and return None. A right rotation moves the final element to index 0 and shifts every other element one position to the right.

Your solution should use constant extra space apart from a fixed number of variables.

Constraints

  • 1 <= len(nums) <= 10^5
  • 0 <= k <= 10^9
  • -10^9 <= nums[i] <= 10^9
  • Modify nums in place and return None

Function Signature

def rotate(nums, k):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output