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.

Google
Coding & Algorithms
Software Engineer
Google
November 29, 2026
Software Engineer
Technical Screen
Coding & Algorithms
Medium

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
Backtracking
Grid DFS
Exhaustive search
In-place marking
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. 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 Problems
Sample 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:

  1. Saves the cell's value and sets it to 0 (visited, and now it looks like rock to neighbors).
  2. Recurses into each neighbor with ore and keeps the best retu...

Submit Your Answer
Markdown supported