Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Rotate Array Coding Challenge

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

Your question is Rotate Array Coding Challenge. 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

In a Tiger Analytics data-processing workflow, records may need to be shifted cyclically before downstream feature preparation. Begin by briefly describing one project you have worked on, then implement an in-place array rotation.

Given an integer array nums and a non-negative integer k, rotate the array to the right by k positions. Modify nums directly and return the same list. Do not create another array proportional to the input size.

A right rotation moves the last element to the first position during each single-step rotation. If k is greater than the array length, treat it modulo the length.

Formal Specification

  • Input: nums, a mutable list of integers, and k, a non-negative integer.
  • Output: The rotated list, with the rotation performed in place.
  • The function must return nums after modification.

Constraints

  • 1 <= len(nums) <= 10^5
  • 0 <= k <= 10^9
  • -10^9 <= nums[i] <= 10^9
  • The rotation must be performed in place
  • Use O(1) auxiliary space

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