The Reaching Points problem asks whether you can reach point (tx, ty) from (sx, sy) using operations: either (x, y) → (x+y, y) or (x, y) → (x, x+y). This hard math problem works backwards — from (tx, ty) → (sx, sy) — to avoid the exponential forward expansion. The math interview pattern is demonstrated.
J.P. Morgan, Goldman Sachs, Microsoft, Amazon, and Google ask this because working backwards with modulo arithmetic turns an exponential problem into an O(log max) problem. It tests the insight that the backwards path is deterministic (only one predecessor exists).
Reverse with modulo. Work backwards: if tx > ty, the previous step was (tx-ty, ty). But instead of subtracting one by one, subtract as many times as possible: tx %= ty (while tx > ty and tx > sx). Similarly for ty > tx. Stop when tx==sx or ty==sy and verify the other coordinate.
sx=1, sy=1, tx=3, ty=5.
sx=1, sy=1, tx=2, ty=2: tx==ty, can only get here from (1,2) or (2,1), not (1,1). Return false.
Reaching Points demonstrates the "reverse and modulo" technique for tree/path problems. Working backwards with modulo converts O(max) to O(log max). The key insight: in reverse, there's only one possible predecessor (either x-y or y-x, whichever is larger was the result of addition). Practice similar reverse-engineering problems: "reverse Euclidean steps," "GCD via subtraction vs modulo."
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Abbreviating the Product of a Range | Hard | Solve | |
| Minimum Moves to Reach Target in Grid | Hard | Solve | |
| Remove 9 | Hard | Solve | |
| Count Total Number of Colored Cells | Medium | Solve | |
| Factorial Trailing Zeroes | Medium | Solve |