Data Structures & AlgorithmsVery hardProduct Company SDE · ICPC regional

Problem: A 3×4 grid holds entry costs (row by row): [[1, 3, 1, 2], [1, 5, 1, 9], [4, 2, 1, 1]]. Starting at the top-left cell, you may move up, down, left or right, and each move costs the value of the cell you enter. The start cell's value is not paid. What is the minimum total cost to reach the bottom-right cell?

  1. A. 7
  2. B. 8
  3. C. 5
  4. D. 22

Correct answer: A. 7

Dijkstra on entry costs finds 7, entering 3, 1 along the top row, then 1, 1 down column 2 and 1 into the corner; a right/down DP agrees here. Also paying for the start cell gives 8, counting moves gives 5, and always stepping to the cheapest unvisited neighbour wanders into expensive cells (22).

More Coding Interview Problems questions

Exams that ask this topic