Understand
30-second explanation
Build the interview-ready mental model before comparing implementations.
Two kangaroos start at positions x1 and x2 and jump v1 and v2 units per jump. They land together after some whole number of jumps n if x1 + n·v1 = x2 + n·v2. Rearranged, n = (x1 − x2) / (v2 − v1). The answer is YES when v1 ≠ v2, the division is exact (no remainder), and the quotient n is non-negative. It is O(1) — one divisibility check, no simulation.
Interview contextWhat is testedWhere candidates slipHow to stand outExplore interview contextHide interview context
What's being tested
Whether you can turn two linear motions into a single equation, solve for the integer jump count, and guard the divide-by-zero (equal speeds) and the negative-quotient (meeting in the past) cases instead of looping blindly.
Where candidates slip
Simulating jumps in a loop with no stop condition so the program never terminates when they never meet, or dividing without checking v1 == v2 first and crashing on divide-by-zero.
How to stand out
Write the meeting equation x1 + n·v1 = x2 + n·v2 on the board, solve for n, and call out both guards — equal speeds and non-negative n — before you touch code.
Choose
Compare the available approaches
Use constraints and trade-offs to select the route you can defend in an interview.
Interactive decision treeInteractiveChoose the right approachFirst decision previewDo two movers ever coincide?Constant speed (linear)?NoYesExplore the full treeCollapse the tree
Learn & trace
Build each solution step by step
Move from the core idea to a visual trace, implementation, proof, and edge cases.
Approach 1 of 2
Simulation — Jump Until They Meet or Diverge
1Hint 1Explore hintHide hint
Model it literally: advance both kangaroos by their speeds each jump and check whether their positions are equal.
2Hint 2Explore hintHide hint
The danger is termination. If the back kangaroo is not faster, the gap never shrinks — your loop must detect that and stop.
3Hint 3Explore hintHide hint
Since x1 < x2, stop the moment the first kangaroo reaches or passes the second; if positions matched on the way, the answer is YES, otherwise NO.
A · Core idea
This is the version you would animate: nudge both kangaroos forward one jump at a time and watch for a collision.
Why a naive loop is dangerous. The obvious code is while (x1 != x2) { x1 += v1; x2 += v2; }. If the kangaroos never meet — say the back one is slower — this loop never exits. Any simulation here must carry an explicit termination condition, which is exactly the subtlety the interviewer is probing.
A correct stop condition. The first kangaroo starts behind (x1 < x2). If it is not strictly faster (v1 <= v2) the gap can never shrink, so we answer immediately: YES only if they already coincide, otherwise NO. When v1 > v2, the gap shrinks every jump, so we advance until the first kangaroo is no longer behind; if it landed exactly on the second at some point, that final position will be equal.
Why it is still inferior. Even with a correct guard, the loop runs proportionally to the starting gap divided by the speed difference — up to ~10⁴ iterations under the constraints. The relationship it is discovering, 'is the gap a multiple of the speed difference?', is a single modulo. Mention the simulation to show you understand the motion, then replace it with the formula.
B · Visual dry run
Interactive dry run
Select a frame to connect the decision with its visual state.
x1 = 0, v1 = 3, x2 = 4, v2 = 2How to read it:When v1 > v2 the loop is guaranteed to end; otherwise the early guard catches it.
YESC · Code with teaching comments
Reference implementation previewpublic String kangaroo(int x1, int v1, int x2, int v2) { if (v1 <= v2) { // back kangaroo can never catch up return x1 == x2 ? "YES" : "NO"; } while (x1 < x2) { // close the gap one jump at a timeExplore the full codeHide the full code
D · Why it works and complexityO(maxGap) timeO(1) spaceOpen the invariant and proofExplore the proofHide the proof
A motion simulation that asks 'do they ever meet?' must always answer 'when do I give up?' first. Recognising that the loop closes the gap by a fixed (v1 − v2) per step is the bridge to the closed-form divisibility test.
ComplexityO(maxGap) time, O(1) space — The first kangaroo can only catch up if v1 > v2, and then it closes the gap by (v1 − v2) per jump, so the loop runs about (x2 − x1)/(v1 − v2) times — linear in the starting gap. Constant extra space.
E · Edge cases and pitfalls3Writing `while (x1 != x2)` with no guard — loops forever when they never meet.Explore edge casesHide edge cases
Pitfalls
- Writing `while (x1 != x2)` with no guard — loops forever when they never meet.
- Forgetting the v1 <= v2 early exit, the only thing keeping the loop bounded.
- Assuming the first kangaroo is always behind without checking — true here by constraint, but state it.
Approach 2 of 2 · Optimal
Divisibility Formula — Solve for n (Optimal)
1Hint 1Explore hintHide hint
Set the positions equal: x1 + n·v1 = x2 + n·v2. Group the n terms and solve for n directly.
2Hint 2Explore hintHide hint
You get n = (x1 − x2) / (v2 − v1). They meet at a real jump count n that must be a whole number and non-negative.
3Hint 3Explore hintHide hint
Guard v1 == v2 first (that makes the denominator zero). Otherwise it is YES exactly when the division has no remainder and the quotient is ≥ 0.
A · Core idea
Solve the meeting condition algebraically instead of stepping through it.
Deriving n. Meeting after n jumps means x1 + n·v1 = x2 + n·v2. Move the position terms to one side and the n terms to the other: x1 − x2 = n·v2 − n·v1 = n·(v2 − v1). Therefore n = (x1 − x2) / (v2 − v1). That single expression is the entire problem — every check below is just validating that this n is a legal jump count.
Guard the denominator. If v1 == v2 the denominator is zero and the division is undefined. Physically, equal speeds mean the gap never changes, so they meet only if they already start together. Since the problem gives x1 < x2, equal speeds always answer NO. Handle this branch before dividing.
The remainder must be zero. n has to be a whole number of jumps. If (x1 − x2) % (v2 − v1) != 0, the kangaroos cross between jumps and never share a position. The modulo is the exact integrality test.
The quotient must be non-negative. Even an exact division can yield a negative n, which would describe a meeting before jumping started — not allowed. Requiring n >= 0 rejects those. Concretely, when the back kangaroo is slower, the formula produces a negative n and we correctly return NO.
Putting it together. YES precisely when v1 != v2, the remainder is 0, and the quotient is ≥ 0. No loop, no termination worries.
B · Visual dry run
Interactive dry run
Select a frame to connect the decision with its visual state.
x1 = 0, v1 = 2, x2 = 5, v2 = 3Interactive diagram
Validating the solved jump count
speeds equal?Three gates — non-zero denominator, zero remainder, non-negative quotient.
NOC · Code with teaching comments
D · Why it works and complexityO(1) timeO(1) spaceOpen the invariant and proofExplore the proofHide the proof
One equation replaces the whole simulation: set positions equal, solve for n, then sanity-check that n is a non-negative integer. The two guards — divide-by-zero and negative quotient — map exactly onto the two physical impossibilities: equal speeds, and the slower kangaroo starting behind.
ComplexityO(1) time, O(1) space — A subtraction, a modulo, a division and two comparisons — a fixed number of operations independent of the inputs. O(1) time and space.
E · Edge cases and pitfalls6x1 = 0, v1 = 2, x2 = 0, v2 = 2 — v1 == v2 and x1 == x2 → YES at n = 0; they already share a spot.Explore edge casesHide edge cases
Edge cases handled
x1 = 0, v1 = 2, x2 = 0, v2 = 2v1 == v2 and x1 == x2 → YES at n = 0; they already share a spot.x1 = 0, v1 = 5, x2 = 3, v2 = 5v1 == v2 but x1 != x2 → NO; equal speeds keep the gap fixed forever.x1 = 14, v1 = 4, x2 = 98, v2 = 2dx = −84, dv = −2, −84 % −2 = 0 and 42 ≥ 0 → YES after 42 jumps.
Pitfalls
- Dividing before checking v1 == v2 — divide-by-zero crash on equal speeds.
- Checking only the remainder and skipping n ≥ 0 — accepts impossible 'past' meetings.
- Relying on language-specific modulo sign behaviour; pairing the remainder test with an explicit quotient sign check keeps it portable.
Practice
Try the solution yourself
Edit the optimal implementation, test another input, and observe the result.
Editable code previewpublic String kangaroo(int x1, int v1, int x2, int v2) { if (v1 == v2) { // denominator would be zero return x1 == x2 ? "YES" : "NO"; } int dx = x1 - x2;Explore the playgroundClose the playground
Avoid mistakes
Recognize the interview traps
Understand why common attempts fail before reviewing the corrected implementation.
1Unbounded simulation
If the kangaroos never meet, `while (x1 != x2)` advances forever. A simulation needs an explicit stop condition.
Explore the code comparisonHide the code comparison
Unbounded simulation
If the kangaroos never meet, `while (x1 != x2)` advances forever. A simulation needs an explicit stop condition.
Don't write this
while x1 != x2: # never exits when they don't meet
x1 += v1
x2 += v2Write this instead
if v1 <= v2:
return "YES" if x1 == x2 else "NO"
while x1 < x2: # bounded: gap shrinks each jump
x1 += v1
x2 += v22Divide-by-zero on equal speeds
v2 − v1 is zero when speeds match. Dividing without guarding crashes; physically equal speeds keep the gap constant.
Explore the code comparisonHide the code comparison
Divide-by-zero on equal speeds
v2 − v1 is zero when speeds match. Dividing without guarding crashes; physically equal speeds keep the gap constant.
Don't write this
int n = (x1 - x2) / (v2 - v1); // crashes when v1 == v2Write this instead
if (v1 == v2) return x1 == x2 ? "YES" : "NO";
int dx = x1 - x2, dv = v2 - v1;3Forgetting the non-negative quotient
An exact division can still be negative, describing a meeting before any jump. Only n ≥ 0 is a real answer.
Explore the code comparisonHide the code comparison
Forgetting the non-negative quotient
An exact division can still be negative, describing a meeting before any jump. Only n ≥ 0 is a real answer.
Don't write this
return (x1 - x2) % (v2 - v1) == 0 ? "YES" : "NO";Write this instead
int dx = x1 - x2, dv = v2 - v1;
return (dx % dv == 0 && dx / dv >= 0) ? "YES" : "NO";Revise
Carry the pattern into the interview
Pattern to remember
Set positions equal, solve for the jump count
Code pattern to memorize
if v1 == v2: return x1 == x2
if (x1 - x2) % (v2 - v1) == 0 and (x1 - x2) // (v2 - v1) >= 0:
return "YES"
return "NO"Rules to remember
- 1Both kangaroos take the same number of jumps n. Meeting means
x1 + n·v1 == x2 + n·v2, which solves ton = (x1 − x2) / (v2 − v1). - 2Equal speeds (v1 == v2) can only meet if they already start together — otherwise the gap never changes. Handle this before dividing to avoid divide-by-zero.
- 3The jump count must be a non-negative integer: the remainder must be 0 and the quotient must be ≥ 0. Skipping the sign check accepts meetings 'in the past'.
- 4The slower-moving kangaroo can never catch the faster one if it started behind — the sign check encodes exactly that.
- 5Do not simulate jumps in an unbounded loop without a stop condition — if they never meet, a naive
whileruns forever.
Use this pattern when
- Two linear motions and you ask whether they ever coincide
- Anything of the form a + n·p == b + n·q for integer n ≥ 0
- A 'do they ever collide / sync up' question over discrete steps
- A closed-form divisibility test replaces an unbounded simulation
Choose another pattern when
- Motion is non-linear (acceleration, varying step) → no clean formula
- You must report when they meet repeatedly, not just whether → model cycles
- More than two movers with pairwise constraints → CRT / LCM territory
- Positions are real-valued, not integers → divisibility no longer applies
Interview takeaway
Whenever two things move at constant speed, 'do they ever meet?' becomes one equation: set the positions equal and solve for the step count. Number Line Jumps reduces to checking that (x1 − x2) is a non-negative multiple of (v2 − v1) — O(1), no loop.
Continue
Transfer the pattern to another problem
Use a direct variation or pattern sibling to prove the idea is reusable.
Transferable patternTwo linear motions meeting reduces to one equation. Set position(t) equal for both movers and solve for the step count; then verify it is a non-negative integer. The two guards — equal rates (no solution) and negative count (solution in the past) — recur in any collision or synchronisation question.
Practice queueDirect variations and pattern siblings
Direct follow-ups