What is the optimal path to maximize rocks collected in a grid?

DSA
Hard
Goldman Sachs
114.5K views

This problem requires finding a path in a 2D grid from bottom-left to top-right that maximizes a specific value, testing dynamic programming skills.

Why Interviewers Ask This

This question evaluates a candidate's ability to apply dynamic programming to optimize a pathfinding problem. It tests the skill of defining states, transitions, and base cases in a matrix. Interviewers look for the ability to break down a complex problem into overlapping subproblems and build up the solution iteratively. It also assesses spatial reasoning and the capacity to reconstruct the actual path taken, not just the maximum value.

How to Answer This Question

Propose a Dynamic Programming approach where dp[i][j] represents the max rocks collected to reach cell (i, j). Define the recurrence relation based on moving only allowed directions (e.g., up or right). Initialize the DP table with base cases for the starting row and column. Iterate through the grid to fill the table. After filling the table, backtrack from the destination to the start to reconstruct the path. Discuss the time complexity O(m*n) and space complexity O(m*n), noting potential optimizations.

Key Points to Cover

  • Define DP state clearly
  • Establish correct recurrence relations
  • Initialize base cases properly
  • Reconstruct the path after computation

Sample Answer

I would solve this using dynamic programming. I define a state dp[i][j] as the maximum rocks collected reaching cell (i, j). The transition involves taking the maximum of the previous valid cells (bottom or left) and add…

Common Mistakes to Avoid

  • Incorrectly initializing the DP table
  • Missing boundary conditions for the first row/column
  • Failing to track the path during reconstruction
  • Confusing the direction of traversal

Sound confident on this question in 5 minutes

Answer once and get a 30-second AI critique of your structure, content, and delivery. First attempt is free — no signup needed.

Try it free

Related Interview Questions

Browse all 107 DSA questionsBrowse all 28 Goldman Sachs questions