VoleonCircular 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.
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()andget_white()(current counts). - Part 2: implement
jump(k), with the same effect as callingstep()ktimes.
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, 1Variants
Kac ring (balls move, marks stay)
Npointsx_0 … x_{N-1}sit clockwise on a circle, each holding a white or black ball;Mof 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
Nand a sorted list of marked points0 ≤ y_1 < … < y_m < N; all balls start white. - Part 1:
step()advances one step;color()returns(W − B) / N, whereW/Bare the white / black counts, and must beO(1). - Part 2:
kstep(k)advancesksteps.- Minority variant: one version asks for
kstep(k)inO(1), another forstep_kinO(n)— clarify the target complexity before coding.
- Minority variant: one version asks for
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 wHere 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 mixingstep()andjump()(or chaining jumps) silently goes wrong. -
Swapping colors whenever
k // nis odd: withmmarked points the lap flips per position arem · (k // n), so an evenmcancels 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 ≥ nexplicitly — laps hit every position equally; only thek % nremainder needs the diff. - Call out the
O(n)scan injump()— fine in general, but a bottleneck ifjump()is called often with smallk.
Follow-ups
Preparation
- Implement
CircularRingand checkstep()three times against the example table. - Verify
jump(k)againstkcalls ofstep()for randomn, marked sets andkfrom 0 to3n, including an even number of marked points. - Hand-trace a wrapped range (
start > end) through the difference array.
