Voleon logoVoleon
Coding·60 minFree preview

Circular Ring Ball Flipping

A ring of n two-sided balls starts all white; each step flips the ball at (x + step) mod n for every marked point x, and you must report black/white counts in O(1). Part 2 adds jump(k), equivalent to k calls of step(), which must avoid O(k·m) replay. The best-known Voleon phone screen; also given as the classic Kac ring, where balls rotate clockwise and flip when leaving a marked point, color() returns (W−B)/N in O(1), and kstep(k) must be fast.

SWE
medium
array
math
simulation
prefix-sum
Frequency
Medium
Last asked
2026-07-08
Stage
phone-screen

Problem

A ring holds n balls, each black on one side and white on the other; all start white. You are given a set of marked points. On step s (counting from 1), for every marked point x the ball at position (x + s) % n is flipped. Counts of black and white balls must be available in constant time.

  • Part 1: implement step() (advance one step), get_black() and get_white() (current counts).
  • Part 2: implement jump(k), with the same effect as calling step() k times.

Examples

Positions are 0-indexed; n = 5, marked points {1, 3}:

start   [W, W, W, W, W]
step 1  [W, W, B, W, B]   # flips 2, 4
step 2  [B, W, B, B, B]   # flips 3, 0
step 3  [B, B, B, B, W]   # flips 4, 1

Variants

Kac ring (balls move, marks stay)

  • N points x_0 … x_{N-1} sit clockwise on a circle, each holding a white or black ball; M of the points are marked. On every step all balls move one point clockwise (x_{N-1} → x_0), and a ball leaving a marked point changes color.
  • The initializer takes N and a sorted list of marked points 0 ≤ y_1 < … < y_m < N; all balls start white.
  • Part 1: step() advances one step; color() returns (W − B) / N, where W / B are the white / black counts, and must be O(1).
  • Part 2: kstep(k) advances k steps.
    • Minority variant: one version asks for kstep(k) in O(1), another for step_k in O(n) — clarify the target complexity before coding.

Print-the-ring version

Write step() and print the colors of all balls after each step; the follow-up is a step_k function in O(n). Example with N = 5, marked positions [1, 3]:

Step 0: w w w w w
Step 1: w b w b w
Step 2: w b b b b
Step 3: b b b w b
Step 4: b w b w w
Step 5: w w w w w

Here step 1 flips the marked positions themselves, one position earlier than the main formulation above — the indexing convention differs across versions.

Hints

Pitfalls

  • Forgetting step_count: jump() must know where the next range starts, otherwise mixing step() and jump() (or chaining jumps) silently goes wrong.

  • Swapping colors whenever k // n is odd: with m marked points the lap flips per position are m · (k // n), so an even m cancels out.

  • Mixed indexing — fix 0- or 1-based up front and trace the example in that convention.

  • Wrapped ranges need both halves written into the diff array.

  • Versions disagree on whether step 1 flips the marked positions themselves or the next ones; pin the convention against the given example first.

  • Candidates describe Part 2 as not hard but fiddly: the prefix-sum idea is easy to name, the wrap-around conditions are where time goes. Some candidates note a bit / XOR parity formulation as an alternative.

  • The phone screen is one hour for both parts, so Part 1 has to go quickly.

Talking Points

  • Step count is state — it anchors where every subsequent range begins.
  • Only parity matters — that is what lets full laps collapse to a single swap and lets ranges be batched.
  • The difference array is the core trick — walk a small wrap-around case by hand so the mechanics are visible.
  • Handle k ≥ n explicitly — laps hit every position equally; only the k % n remainder needs the diff.
  • Call out the O(n) scan in jump() — fine in general, but a bottleneck if jump() is called often with small k.

Follow-ups

Preparation

  • Implement CircularRing and check step() three times against the example table.
  • Verify jump(k) against k calls of step() for random n, marked sets and k from 0 to 3n, including an even number of marked points.
  • Hand-trace a wrapped range (start > end) through the difference array.
Was this article helpful?

Comments

Sign in to join the discussion
Loading...