Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Clockwise Array Rotation

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

Your question is Clockwise Array Rotation. 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

Sam Houston State University systems may need to shift a sequence of records so the newest entries appear first. Given an integer array and a nonnegative rotation count, rotate the array clockwise, meaning to the right, by k positions.

Modify the input array in place and return it. Aim for linear time and constant extra space. Rotating by more than the array length should produce the same result as rotating by k % len(nums) positions.

Formal Specification

Implement rotate_array(nums, k).

  • Input: nums, a list of integers, and k, a nonnegative integer.
  • Output: The same list object after its elements have been rotated right by k positions.
  • The relative order of all elements must be preserved after rotation.

Constraints

  • 0 <= nums.length <= 10^5
  • -10^9 <= nums[i] <= 10^9
  • 0 <= k <= 10^9
  • Use O(1) auxiliary space, excluding the input array

Function Signature

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