Magicsheet logo

Moving Stones Until Consecutive

Medium
25%
Updated 8/1/2025

Asked by 1 Company

Moving Stones Until Consecutive

What is this problem about?

The Moving Stones Until Consecutive problem places three stones at distinct positions a, b, c on a number line. In each move, you can pick an endpoint stone and move it to any position strictly between the other two. Find the minimum and maximum number of moves to make the stones consecutive. This Moving Stones Until Consecutive coding problem is a math and brainteaser problem with clean case analysis.

Why is this asked in interviews?

Meta asks this because it tests mathematical case analysis and the ability to reason about a physical process without simulation. The minimum and maximum move counts follow from simple conditions about the gaps between stones. The brainteaser and math interview pattern is directly demonstrated.

Algorithmic pattern used

Case analysis after sorting. Sort: a ≤ b ≤ c.

  • Maximum moves: Fill all empty spaces. Max = (b-a-1) + (c-b-1) (move each stone from one end toward the middle one step at a time, always leaving at least one gap).
  • Minimum moves: If already consecutive, 0 moves. If any gap ≤ 2 or b is already a+1 or c-1 (one move closes the gap), minimum = 1. Otherwise, minimum = 2.

Example explanation

Stones at [1, 2, 5]. Sorted: a=1, b=2, c=5.

  • Max = (2-1-1) + (5-2-1) = 0 + 2 = 2.
  • Min: c-a = 4, not consecutive. Gap b-a=1 (consecutive already), gap c-b=3. Since b-a ≤ 2, min = 1.

Stones at [3, 5, 10]. Max = 1+4=5. Min = 2 (no gap ≤ 2).

Common mistakes candidates make

  • Simulating the moves step by step instead of applying the formula.
  • Confusing minimum moves case: checking only one gap instead of both.
  • Off-by-one: forgetting "strictly between" means gaps of exactly 1 don't allow a move.
  • Not handling already-consecutive case separately (min=0, max=0).

Interview preparation tip

Math brainteaser problems require pen-and-paper reasoning before coding. For "minimum/maximum operations to reach a configuration," work out 3-5 examples by hand to identify patterns. Here: max is straightforward (fill both gaps), minimum requires recognizing when one move is enough (a gap ≤ 2 means you can jump over with one move). Practicing case analysis on small problems sharpens this skill quickly.

Similar Questions