Find minimum cost in grid

Last updated: October 17, 2025

Quick Overview

Given a 2D grid where each cell contains a cost value, write a function to find the minimum cost to travel from the top-left corner to the bottom-right corner, moving only down or right. The function should return the minimum total cost as an integer. The grid will be represented as a list of lists of integers.

Morgan Stanley
Coding & Algorithms
Software Engineer
Morgan Stanley
October 17, 2025
Software Engineer
Phone Screen
Coding & Algorithms
Medium

5

14

4,148 solved


Given a 2D grid where each cell contains a cost value, write a function to find the minimum cost to travel from the top-left corner to the bottom-right corner, moving only down or right. The function should return the minimum total cost as an integer. The grid will be represented as a list of lists of integers.

Morgan Stanley uses this problem in the Phone Screen to evaluate your algorithmic thinking. They expect you to discuss multiple approaches, analyze trade-offs between them, and implement the optimal solution with clean, readable code.

What the Interviewer Expects
  • Recognize the underlying problem pattern (sliding window, two pointers, BFS/DFS, etc.)
  • Discuss multiple approaches and trade-offs before coding
  • Implement an optimal solution with clean, production-quality code
  • Handle all edge cases including boundary conditions and invalid input
  • Optimize both time and space complexity with clear justification
  • Test your solution systematically with well-chosen examples
Key Topics to Cover
Common algorithm patterns (sliding window, two pointers, BFS/DFS)
Sorting and searching
Dynamic programming and memoization
Time and space complexity analysis
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
  • How would you test this solution thoroughly?
  • Can you solve this iteratively instead of recursively (or vice versa)?
  • How would you modify your solution to handle streaming input?
  • What is the worst-case input for your solution?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Problem Analysis

The problem requires finding the minimum cost path in a 2D grid from the top-left corner to the bottom-right corner, moving only down or right. This scenario is best approached using Dynamic Programmi...

Approach

We will create a DP table where each cell (i, j) represents the minimum cost to reach that cell. The steps are as follows:

  1. Initialize a DP table of the same size as the grid.
  2. Set the starting po...

Submit Your Answer
Markdown supported

Related Questions