Reaching Points

HardMathmath

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

Starting from a point (x, y), one move replaces it with either (x + y, y) or (x, x + y). Given a start (sx, sy) and a target (tx, ty), all positive integers, return true if the target can be reached from the start using zero or more moves, and false otherwise. Coordinates go up to 10^9, so searching forward from the start is far too slow.

Examples

Input: sx = 1, sy = 1, tx = 3, ty = 5
Output: true
`(1,1) -> (1,2) -> (3,2) -> (3,5)`.
Input: sx = 2, sy = 3, tx = 5, ty = 6
Output: false
Working backwards, `(5,6)` can only come from `(5,1)`, which already has `y` below 3.
Input: sx = 4, sy = 4, tx = 4, ty = 4
Output: true
Zero moves are needed.

Constraints

  • `1 <= sx, sy, tx, ty <= 10^9`

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More math problems