Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Rotate Prime Array

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

Your question is Rotate Prime Array. 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 Research Group needs to reorder a sequence of prime-number identifiers in place while preserving their relative circular order. Given an array containing only prime numbers and a non-negative rotation count, rotate the array to the right by k positions.

A right rotation moves each element to index (i + k) % n. The function must modify the input array in place and return the same array. The fact that every value is prime is an input guarantee, not a condition that requires additional primality checks.

Formal Specification

Implement rotate_primes(nums, k):

  • Input: nums, a mutable list of prime integers, and k, a non-negative integer.
  • Output: The rotated list, returned after modifying nums in place.
  • If k is greater than the array length, rotations repeat cyclically.

Constraints

  • 1 <= len(nums) <= 10^5
  • Every element of nums is a prime integer between 2 and 10^9
  • 0 <= k <= 10^9
  • The input array 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