Collect the Most Ore From a Mine Without Revisiting Tunnels
Last updated: November 29, 2026
Quick Overview
A mine grid stores the ore in each cell, with 0 meaning solid rock. Starting from any cell with ore and moving in four directions through cells with ore, never revisiting a cell, return the maximum total ore you can collect.
0
0
0 solved
A mine grid stores the ore in each cell, with 0 meaning solid rock. Starting from any cell with ore and moving in four directions through cells with ore, never revisiting a cell, return the maximum total ore you can collect.
A Google technical-screen grid problem where greedy fails and every start must be tried. The interviewer checks in-place marking with restore, trying all starting cells, and a sensible argument for why the search is bounded.
What the Interviewer Expects
- Tries every non-zero cell as a starting point
- Marks the cell by zeroing it during the DFS and restores it afterward
- Takes the best over all four neighbors at each step
- Explains why greedy best-neighbor fails
- Bounds the work using the limit on ore cells
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- Prune starting cells that have three or four ore neighbors, since an endpoint never needs them. Why does that work?
- How would you return the path itself?
- What if you may revisit cells but collect ore only once?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
- Input: m by n grid of non-negative ints, m, n <= 15, with at most 25 non-zero cells.
- Output: the maximum sum of a simple path through non-zero cells.
- Example:
[[0,6,0],[5,8,7],[0,9,0]]gives 2...
High-Level Design
For every cell with ore, run a DFS that:
- Saves the cell's value and sets it to 0 (visited, and now it looks like rock to neighbors).
- Recurses into each neighbor with ore and keeps the best retu...