Reported September 2022
IMCbreadth first search

Reaching Points with Perfect-Square Obstacles

Reported by candidates from IMC's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live IMC OA. Under 2s to a working solution.
Founder's read

The IMC OA reported in September 2022 looks like a nasty number-theory puzzle about reaching points, but it's a reachability search on a small grid. Coordinates cap at 1000, the moves only go up, and the forbidden cells are just sums that are perfect squares. If you've got an invite for this one, the work is spotting that the scary framing hides a plain DP or BFS. StealthCoder sits invisibly on your screen as a safety net if you blank mid-assessment, but the idea is simple enough to hold in your head.

The problem

A bot starts at the nonnegative integer coordinate (startX, startY) and wants to reach (targetX, targetY). A positive constant c is fixed for the entire journey.
From a coordinate (x, y), the bot may make exactly one of these moves:
Move to (x + y, y).
Move to (x, x + y).
Move to (x + c, y + c).
A coordinate is forbidden when x + y is a perfect square. The bot may never occupy a forbidden coordinate. This rule also applies to the starting and target coordinates.
Return "Yes" if a sequence of legal moves reaches the target. Otherwise, return "No".

Function
canReach(c: int, startX: int, startY: int, targetX: int, targetY: int) → String

Examples
Example 1
c = 1
startX = 2
startY = 1
targetX = 3
targetY = 5
return = "Yes"
The bot can move from (2, 1) to (3, 2) using the third move, then to (3, 5) using the second move. Neither visited sum is a perfect square.
Example 2
c = 2
startX = 2
startY = 7
targetX = 10
targetY = 12
return = "No"
The starting sum is 2 + 7 = 9, a perfect square, so no path is allowed.

Constraints
0 <= startX, startY, targetX, targetY <= 1000
1 <= c <= 15
Coordinates never decrease. On an axis, a move can leave the position unchanged.
If either endpoint's coordinate sum is a perfect square, the answer is "No". This includes sum 0.

Reported by candidates. Source: FastPrep

Pattern and pitfall

What it reduces to: a forward reachability check over a 1001 by 1001 grid. Coordinates never decrease, so you can mark reachable cells with a visited array and either BFS from the start or iterate in increasing order. Precompute perfect squares up to 2000 in a set. Check the start and target first. If either sum is a square, including 0, answer No immediately. Then from each reachable cell, try (x+y, y), (x, x+y) and (x+c, y+c), skip anything out of bounds or forbidden, and mark it visited. The pitfall is the zero case. A cell like (0,0) has sum 0, which is a square, and a move from (x,0) to (x,x) can keep things in place. Skip moves that don't change anything so you don't loop. If you freeze on the live OA, StealthCoder can hand you the BFS skeleton, but you only need the visited grid and the square set.

StealthCoder is the hedge for the one pattern you didn't drill. It runs invisibly during the screen share.

If this hits your live OA

You can drill Reaching Points with Perfect-Square Obstacles cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. If you're reading this with an OA window open, you're who this was built for.

Get StealthCoder

Related leaked OAs

⏵ The honest play

You've seen the question. Make sure you actually pass IMC's OA.

IMC reuses patterns across OAs. If you're reading this with an OA window open, you're who this was built for. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Reaching Points with Perfect-Square Obstacles FAQ

What's the trick in the IMC reaching points problem?+

Don't reason about the math. Treat it as reachability on a grid capped at 1000 by 1000. Precompute perfect squares, reject forbidden endpoints, then BFS or DFS with a visited array using the three moves. Bounds keep it fast.

Why does the answer return No for example 2?+

The start is (2, 7) and 2 + 7 = 9, which is a perfect square. Starting on a forbidden coordinate means no path is legal, so you return No before searching anything.

Do I need to handle sum 0 specially?+

Yes. Zero counts as a perfect square here, so a start or target at (0, 0) returns No. Put this check up front with the other endpoint checks, and make sure your square set includes 0.

How do I avoid infinite loops?+

Use a visited grid and skip any move that lands on the same cell or outside the 0 to 1000 range. A move like (x + y, y) with y = 0 doesn't change position, so the visited check handles it cleanly.

How should I prepare in 48 hours for this kind of OA?+

Practice grid reachability with BFS and DFS, and get comfortable building a lookup set of precomputed values like squares. Read constraints first. Small bounds like 1000 and c up to 15 usually mean brute-force search is intended.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with IMC.

OA at IMC?
Invisible during screen share
Get it