Skip to content
    SeaCode
    Learning tracksCode ArenaWorkshops & EventsInstructors
    Log inSign up
    Practice problems / Shortest path in a maze

    Shortest path in a maze

    Mediumgraphbfsmatrix

    grid is a maze: "0" is open and "1" is a wall. You move up/down/left/right. Return the fewest cells on a path from the top-left cell to the bottom-right cell (both counted), or -1 if there's no path.

    Example 1

    Input:  grid = ["00","10"]
    Output: 3

    (0,0) → (0,1) → (1,1): 3 cells.

    Example 2

    Input:  grid = ["010","010","000"]
    Output: 5

    Example 3

    Input:  grid = ["01","10"]
    Output: -1

    Constraints

    • 1 ≤ rows, cols ≤ 100
    • Each cell is "0" or "1"

    Submit checks your solution against 9 tests, including hidden ones.

    /**
     * @param {string[]} grid
     * @return {int}
     */
    function shortestPath(grid) {
      
    }
    
    Sign in to run
    SeaCode

    Learn to code for real, online or at our branch in front of Cairo University, Giza.

    Learn

    • All courses
    • Interactive tutorials
    • Interview practice problems
    • Mock interview
    • Learning tracks
    • Code Arena
    • Workshops & Events
    • Knowledge library
    • Team projects

    Your account

    • Log in
    • Sign up
    • Learning plan
    • Certificates
    • Site map

    Contact us

    • WhatsApp
    • 010 9094 1104
    • In front of Cairo University, Giza

    © 2026 SeaCode. All rights reserved.