HARDasked at 1 company

Minimum Cost to Make at Least One Valid Path in a Grid

A hard-tier problem at 71% community acceptance, tagged with Array, Breadth-First Search, Graph. Reported in interviews at Cleartrip and 0 others.

Founder's read

You're given a grid where each cell points to an adjacent direction, and moving against that arrow costs 1. You need the minimum cost to reach the bottom-right corner from top-left. This is a shortest-path problem disguised as a grid navigation puzzle. Cleartrip has asked it. The acceptance rate is above 70 percent, but that masks a real spike: most solvers either brute-force with DFS (too slow) or miss that this is a 0-1 BFS problem, not standard Dijkstra. If this hits your live assessment and the naive recursion times out, StealthCoder surfaces the correct deque-based approach in seconds, invisible to the proctor.

Companies asking
1
Difficulty
HARD
Acceptance
71%

Companies that ask "Minimum Cost to Make at Least One Valid Path in a Grid"

If this hits your live OA

Minimum Cost to Make at Least One Valid Path in a Grid is the kind of problem that decides whether you pass. StealthCoder reads the problem on screen and surfaces a working solution in under 2 seconds. Invisible to screen share. The proctor sees nothing. Made by an Amazon engineer who watched the leaked-problem repo become an industry secret. He decided you should have it too.

Get StealthCoder
What this means

The trick is recognizing that edge weights are binary: following the cell's arrow costs 0, going any other direction costs 1. Standard Dijkstra overkill here. You want 0-1 BFS: use a deque, add zero-cost moves to the front and cost-1 moves to the back. This guarantees you process nodes in optimal order without a heap. Common pitfall: treating it as a uniform-cost shortest path and reaching for Dijkstra, then timing out on large grids. Another trap: recursive DFS with memoization that hits the call stack. The array itself is just bookkeeping once you recognize the graph structure. If you freeze on the transition from 'which direction is free?' to 'how do I search it efficiently?', that's where StealthCoder cuts through the noise and shows the deque pattern in context.

Pattern tags

The honest play

You know the problem. Make sure you actually pass it.

Minimum Cost to Make at Least One Valid Path in a Grid recycles across companies for a reason. It's hard-tier, and most candidates blank under the timer. StealthCoder is the hedge: an AI overlay invisible during screen share. It reads the problem and surfaces a working solution in under 2 seconds. Made by an Amazon engineer who watched the leaked-problem repo become an industry secret. He decided you should have it too. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Minimum Cost to Make at Least One Valid Path in a Grid interview FAQ

Is this really a hard problem or just a shortest-path rehash?+

It's hard because the graph structure is implicit in the direction array, not given explicitly. The puzzle is pattern recognition: spotting that 0-1 BFS applies here, not general Dijkstra. Most candidates miss this and implement the slower algorithm. Once you see it, the code is 20 lines.

Why does DFS with memoization fail?+

DFS explores all directions recursively, leading to exponential time in the worst case even with memoization. You also risk stack overflow on large grids. 0-1 BFS processes each cell once, guaranteed. Different algorithmic paradigm entirely.

How does this relate to graph and matrix topics?+

The grid is a weighted graph where edge weight depends on whether you follow or defy the cell's arrow. Matrix is the input encoding. Graph is the abstraction you need to apply. Shortest Path is the core algorithm. All four topics converge here.

What's the time and space complexity?+

Time is O(rows x cols) because each cell is visited once via deque BFS. Space is O(rows x cols) for the distance table and queue. No heap overhead. That's why 0-1 BFS beats Dijkstra on this specific structure.

Has Cleartrip asked this recently or is it stale?+

The data shows Cleartrip as a reported asker. Given the 70% acceptance rate and the pattern of the problem, it's still active in their pipeline. It's the kind of optimization puzzle tech companies use to filter for algorithmic depth.

Want the actual problem statement? View "Minimum Cost to Make at Least One Valid Path in a Grid" on LeetCode →

Frequency and company-tag data sourced from public community-maintained interview-report repos. Problem, description, and trademark © LeetCode. StealthCoder is not affiliated with LeetCode.