0
0
DSA Typescriptprogramming~20 mins

Minimum Path Sum in Grid in DSA Typescript - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Minimum Path Sum Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Minimum Path Sum Calculation
What is the output of the following TypeScript code that calculates the minimum path sum in a 3x3 grid?
DSA Typescript
function minPathSum(grid: number[][]): number {
  const m = grid.length;
  const n = grid[0].length;
  for (let i = 1; i < m; i++) {
    grid[i][0] += grid[i - 1][0];
  }
  for (let j = 1; j < n; j++) {
    grid[0][j] += grid[0][j - 1];
  }
  for (let i = 1; i < m; i++) {
    for (let j = 1; j < n; j++) {
      grid[i][j] += Math.min(grid[i - 1][j], grid[i][j - 1]);
    }
  }
  return grid[m - 1][n - 1];
}

const grid = [
  [1, 3, 1],
  [1, 5, 1],
  [4, 2, 1]
];
console.log(minPathSum(grid));
A8
B7
C9
D6
Attempts:
2 left
💡 Hint
Trace the path from top-left to bottom-right, adding the smallest sums step by step.
🧠 Conceptual
intermediate
1:00remaining
Understanding the Movement Constraints
In the Minimum Path Sum problem, which movements are allowed when moving from the top-left corner to the bottom-right corner of the grid?
AOnly moves diagonally
BMoves in all four directions: up, down, left, right
COnly moves to the right or down
DMoves to the right, down, and diagonally
Attempts:
2 left
💡 Hint
Think about the problem constraints that simplify the path choices.
🔧 Debug
advanced
2:00remaining
Identify the Error in the Minimum Path Sum Code
What error will the following TypeScript code produce when run, and why?
DSA Typescript
function minPathSum(grid: number[][]): number {
  const m = grid.length;
  const n = grid[0].length;
  for (let i = 1; i <= m; i++) {
    grid[i][0] += grid[i - 1][0];
  }
  for (let j = 1; j <= n; j++) {
    grid[0][j] += grid[0][j - 1];
  }
  for (let i = 1; i < m; i++) {
    for (let j = 1; j < n; j++) {
      grid[i][j] += Math.min(grid[i - 1][j], grid[i][j - 1]);
    }
  }
  return grid[m - 1][n - 1];
}

const grid = [
  [1, 3, 1],
  [1, 5, 1],
  [4, 2, 1]
];
console.log(minPathSum(grid));
ATypeError: Cannot read property '0' of undefined
BSyntaxError: Unexpected token
CReturns incorrect minimum path sum
DNo error, outputs 7
Attempts:
2 left
💡 Hint
Check the loop conditions and array indexing carefully.
🚀 Application
advanced
2:30remaining
Minimum Path Sum in a Larger Grid
Given the following 4x4 grid, what is the minimum path sum from top-left to bottom-right?
DSA Typescript
const grid = [
  [1, 2, 5, 2],
  [3, 2, 1, 4],
  [4, 3, 2, 1],
  [5, 6, 1, 1]
];

function minPathSum(grid: number[][]): number {
  const m = grid.length;
  const n = grid[0].length;
  for (let i = 1; i < m; i++) {
    grid[i][0] += grid[i - 1][0];
  }
  for (let j = 1; j < n; j++) {
    grid[0][j] += grid[0][j - 1];
  }
  for (let i = 1; i < m; i++) {
    for (let j = 1; j < n; j++) {
      grid[i][j] += Math.min(grid[i - 1][j], grid[i][j - 1]);
    }
  }
  return grid[m - 1][n - 1];
}

console.log(minPathSum(grid));
A10
B15
C14
D12
Attempts:
2 left
💡 Hint
Calculate cumulative sums row-wise and column-wise, then fill the rest using minimum of top and left cells.
Predict Output
expert
3:00remaining
Output of Minimum Path Sum with Negative Values
What is the output of the following TypeScript code that calculates the minimum path sum in a grid containing negative values?
DSA Typescript
function minPathSum(grid: number[][]): number {
  const m = grid.length;
  const n = grid[0].length;
  for (let i = 1; i < m; i++) {
    grid[i][0] += grid[i - 1][0];
  }
  for (let j = 1; j < n; j++) {
    grid[0][j] += grid[0][j - 1];
  }
  for (let i = 1; i < m; i++) {
    for (let j = 1; j < n; j++) {
      grid[i][j] += Math.min(grid[i - 1][j], grid[i][j - 1]);
    }
  }
  return grid[m - 1][n - 1];
}

const grid = [
  [2, -3, 1],
  [-5, 4, -2],
  [3, -1, 2]
];
console.log(minPathSum(grid));
A-1
B1
C-2
D0
Attempts:
2 left
💡 Hint
Consider how negative values affect the cumulative sums and path choices.