Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
Step_K Time-Driven Ball Flip
00:00
5 left

Step_K Time-Driven Ball Flip

MediumPython

Problem

Given n balls with initial positions, all white; after each step, balls on marked positions flip (white to black or black to white) and every position advances by 1 (e.g. N=5, marked positions [1,3]). Write a step_k function with O(n) time complexity.

The technical phone screen uses five cyclic positions, numbered 0 through 4. At each step, determine which balls currently occupy a marked position, flip those balls, then advance every ball by one position modulo 5.

Contract

Implement step_k(initial_positions, marked_positions, k). Return a list aligned with initial_positions, where each item is [final_position, final_color]. Every ball starts white, and k may be zero or very large.

Constraints

  • 1 <= len(initial_positions) <= 100
  • 0 <= initial_positions[i] <= 4
  • 0 <= marked_positions[i] <= 4
  • marked_positions contains distinct positions
  • 0 <= k <= 10^18

Function Signature

def step_k(initial_positions, marked_positions, k):
Interviewer

Your question is Step_K Time-Driven Ball Flip. Start with the requirements in the Question tab.

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.
CodePython 3
You need to log in / sign up to run or submit.Ln 2
Run your code to see test output here.