Rock Jumping
Reported by candidates from IMC's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The edge case in the IMC Rock Jumping question, reported in August 2026, is the one that sinks a quick solution: what happens when the water level never matters, or when even zero water can't get you across. If you're taking this OA soon, know that it's a binary search on the water height with a shortest-path check inside. Rocks are sorted by x, the cost is squared distance, and the limits go up to 10^18. Miss the return values of -1 and 10^9 and you fail hidden tests. StealthCoder sits invisibly on your screen as a safety net if you blank during the live assessment.
The problem
You are standing on the left bank of a river that is width units wide, and you are aiming to reach the right bank. The left bank is located at x = 0 and the right bank is located at x = width. There are several rocks in the river at various x-coordinates between the banks, each with a specific height. You can jump between rocks or directly to the right bank. The cost to jump between two locations is the square of the distance between them: (x[i] - x[j])^2. The water level is rising over time. Once the water reaches a certain level, some rocks will be submerged and can no longer be used for jumping. A rock is considered submerged if the water level is higher than its height. The water stops rising once you start your first jump. There is a maximum jump distance maxJump and a maximum total energy maxEnergy that you can use to cross the river. The total energy cost of all jumps must not exceed maxEnergy. Determine the maximum water height at which you can still reach the other side of the river without exceeding maxJump or maxEnergy. You are provided with four integers: width, the width of the river; numRocks, the number of rocks; maxJump, the maximum jump distance; and maxEnergy, the maximum total energy. You are also provided with two integer arrays: x, containing the x-coordinate of each rock, and heights, containing the height of each rock. Rocks are located strictly between the banks, so 0 < x[i] < width. Return a single integer representing the maximum water height at which you can still reach the other side of the river. If it is impossible to reach the other side, return -1. If it will always be possible to reach the other side, return 10^9. Function maxWaterHeight(width: int, numRocks: int, maxJump: int, maxEnergy: long, x: int[], heights: int[]) → int Examples Example 1 width = 10 numRocks = 2 maxJump = 10 maxEnergy = 40 x = [4, 7] heights = [3, 5] return = 3 Constraints 1 <= width, maxJump <= 10^9 1 <= maxEnergy <= 10^18 1 <= numRocks <= 10^5 0 < x[i] < width 0 <= heights[i] <= 10^9 x is in ascending order.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is monotonicity. A higher water level only removes rocks, so if you can cross at height h, you can cross at any lower height. That means binary search on h. For each candidate h, keep only rocks with height >= h, then run a DP over the sorted positions: dp[j] = min over i of dp[i] + (x[j]-x[i])^2 where x[j]-x[i] <= maxJump. The banks at 0 and width are always usable. Check dp[width] <= maxEnergy. The pitfall is the edge cases. Test h = 0 first. If it fails, return -1. If it passes at the largest rock height, no rock matters beyond that, so return 10^9. Also watch overflow: squared distances reach 10^18, so use 64-bit and cap sums. A naive O(n^2) DP per check will time out at n = 10^5. Use the maxJump window or a convex hull trick. If the live OA freezes you on that optimization, StealthCoder is the hedge.
If you see this problem in your OA tomorrow, the play is to recognize the pattern in 30 seconds. StealthCoder buys you that recognition.
You can drill Rock Jumping 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. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass IMC's OA.
IMC reuses patterns across OAs. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Rock Jumping FAQ
What's the core trick in IMC Rock Jumping?+
Binary search on the water height. Raising the water only removes rocks, so feasibility is monotonic. For each guess, filter rocks by height and compute the minimum energy path from 0 to width. Compare that cost to maxEnergy and move the search bounds.
When do I return -1 versus 10^9?+
Return -1 if you can't cross even at water level 0, meaning all rocks are usable and the energy or jump limit still fails. Return 10^9 if you can cross with every rock removed, since then water height never matters. Check both before the search.
How hard is this one really?+
Medium-hard. The binary search idea is easy to spot. The hard part is the inner check at n up to 10^5, plus overflow with maxEnergy up to 10^18. Squared distances can hit 10^18, so use 64-bit integers and be careful adding them.
Why does the inner shortest path need care?+
A plain O(n^2) DP is too slow across about 30 binary search steps. The squared cost makes it a convex cost transition, so a monotone pointer window limited by maxJump or a convex hull style optimization brings each check closer to linear or n log n.
How do I prepare in 48 hours?+
Write binary search on the answer with a feasibility function from scratch. Then practice a 1D DP with a distance-limited window. Test edge cases: one rock, all rocks the same height, maxJump smaller than any gap, and maxEnergy exactly equal to the best cost.