Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Rotate Prime Numbers by Input

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

Your question is Rotate Prime Numbers by Input. 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

PCS Global Tech processes ordered prime-number sequences. Given an array nums containing prime numbers and a non-negative rotation count k, rotate the array to the right by k positions and return the modified array.

The rotation must be performed in place, without creating another array proportional to nums.length. The values are guaranteed to be prime, so do not validate primality.

Formal Specification

Implement rotate_primes(nums, k):

  • Input: nums, a mutable list of distinct or repeated positive prime integers, and k, a non-negative integer.
  • Output: The same list object after a right rotation by k positions.
  • A right rotation moves the final element to index 0 and shifts every other element one position right.
  • If k exceeds the array length, equivalent rotations should be used.

Constraints

  • 0 <= nums.length <= 10^6
  • nums[i] is a positive prime integer less than 10^9
  • 0 <= k <= 10^18
  • The input list must be modified in place
  • Use O(1) auxiliary space

Function Signature

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