Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Shortest Path in Cost Maze

MediumPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Shortest Path in Cost Maze. Start with the requirements on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

Given a 2D grid maze of non-negative integers, find the minimum total cost to move from the top-left cell (0, 0) to the bottom-right cell (m-1, n-1). You may move up, down, left, or right. Entering a cell adds that cell’s cost to the path total, including the starting and ending cells. Return -1 if the destination cannot be reached.

Constraints

  • 1 <= m, n <= 200
  • maze[i][j] is either -1 or an integer in [0, 10^6]
  • Movement is allowed only in the 4 cardinal directions
  • Return -1 if the start or destination is blocked

Function Signature

def min_cost_maze_path(maze):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output