Prepin
Log in

Determine the lowest cost required to travel from the first cell of the matrix to the last cell.

Approach / Explanation

The interview question requires you to devise an algorithm. This task involves creating an algorithm that can navigate through a specified matrix (a 2D array of integers). Starting from the first cell (located at the top-left corner), the algorithm must reach the last cell (at the bottom-right corner) while following the path with the lowest cumulative cost. The cost is determined by the value in each matrix cell, and the algorithm is permitted to move either to the right or downward from a given cell. Many problems of this nature can be approached using Dynamic Programming (DP) techniques. Here are some important considerations when tackling this issue: Identify your base case, which is typically what you can determine without further calculations. In this scenario, it would be the cost of the first cell itself, as there is only one route to it. Next, consider how you will populate the remainder of your matrix or array. The cost of each cell can be calculated as the minimum of the costs of the cell above it and the cell to its left, plus the cost of the current cell. Aim to solve the problem in a bottom-up manner, addressing and storing solutions for subproblems first, and then using those results to tackle the overarching problem.

Suggested Answer

We can begin at the top-left cell of the matrix and can move either to the right or downward. Thus, the cost associated with any cell is determined by adding the cost of the current cell to the lesser cost of the cell directly to its left or the cell directly above it. Here’s how to implement this in Python: def minCost ( matrix ) : rows , cols = len ( matrix ) , len ( matrix [ 0 ] ) for i in range ( 1 , rows ) : matrix [ i ] [ 0 ] += matrix [ i - 1 ] [ 0 ] for j in range ( 1 , cols ) : matrix [ 0 ] [ j ] += matrix [ 0 ] [ j - 1 ] for i in range ( 1 , rows ) : for j in range ( 1 , cols ) : matrix [ i ] [ j ] += min ( matrix [ i - 1 ] [ j ] , matrix [ i ] [ j - 1 ] ) return matrix [ - 1 ] [ - 1 ] This algorithm optimizes the process by storing intermediate results directly in the input matrix, resulting in a cost matrix where each cell indicates the minimum cost to reach that specific cell from the starting cell.

Alternative Answer

In a similar vein, if you were to implement this in Java, your code might resemble the following: public static int minCost ( int [ ] [ ] cost ) { int m = cost . length ; int n = cost [ 0 ] . length ; for ( int i = 1 ; i < m ; i ++ ) { cost [ i ] [ 0 ] = cost [ i - 1 ] [ 0 ] + cost [ i ] [ 0 ] ; } for ( int j = 1 ; j < n ; j ++ ) { cost [ 0 ] [ j ] = cost [ 0 ] [ j - 1 ] + cost [ 0 ] [ j ] ; } for ( int i = 1 ; i < m ; i ++ ) { for ( int j = 1 ; j < n ; j ++ ) { cost [ i ] [ j ] = cost [ i ] [ j ] + Math . min ( cost [ i - 1 ] [ j ] , cost [ i ] [ j - 1 ] ) ; } } return cost [ m - 1 ] [ n - 1 ]; } Both the Python and Java implementations fundamentally achieve the same objective, although Python benefits from simplified access to the last element through -1 indexing. This code serves as a dynamic programming solution, efficiently determining the minimum cost needed to reach the final cell.

Question Details

Difficulty & Category

Medium

Product

For CandidatesFor RecruitersCompaniesBrowse JobsInterview QuestionsDeep ProfileSkill AssessmentOpportunity Matching
Prepin.ai

© 2026 Prepin LLC · Philadelphia, PA · All rights reserved.