Implement Kac Ring Simulation with O(1) Step Query
Implement a Kac Ring data structure consisting of a circular ring of N positions, each with a color (0 or 1, or equivalently White/Black) and optional markers. When the simulation advances one step, each ball/position moves clockwise, and any position leaving a marked spot flips its color. Implement two methods: step() to advance the simulation by one step, and kstep(k) to advance by k steps, each returning the color ratio (number of 1s / N, or equivalently (W - B) / N) after the operation. Optimize from a brute-force O(N) per step approach to an O(1) per query solution using prefix sums or similar techniques.
Question Timeline
See when this question was last asked and where, including any notes left by other candidates.
Early October, 2026
Early August, 2026
Late March, 2026
Problem: • Npositions on a circular ring (O.N-1), each with a color (0 or 1) and a marker (some positions have markers). • Rule: When the cursor steps clockwise by 1, every position with a marker flips its color. • Part 1: After a single step, compute the ratio of 1s in the ring. (Brute force O(N) accepted.) • Part 2: After stepping k positions, compute the ratio in O(1) per query using prefix sums.
Hello Interview Premium
Your account is free and you can post anonymously if you choose.