Dataford
Interview QuestionsInterview GuidesExperiencesCoaching
New
Pricing
Get started
Dataford
Popular roles
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Browse
Browse All RolesEvery role hub, from analyst to MLBrowse All CompaniesCompany-specific interview loopsAll Interview GuidesThe full guide library
Top questions by role
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Top questions by skill
SQLPythonStatisticsMachine LearningA/B TestingSystem DesignGenerative AIProduct SenseMetricsBehavioral
Browse all questions →Try a mock interview
Experiences
Practice
Mock InterviewsTimed interview simulations with feedbackCoaching1:1 coaching with people at your target companiesSuccess PathYour 6-week structured planModulesCurated lessons by topicWebinarsTalks from ex-Big Tech data leadsPlaygroundA free-form scratch editor
Learn
BlogInterview strategy and career adviceTech Job Market ReportHiring trends across data and AI rolesFor UniversitiesDataford for career centersAbout DatafordWho we are and how we build
Pricing
Build my plan
Rat Maze Traversal
00:00
5 left

Rat Maze Traversal

MediumPython

Problem

An Airtel network planning grid is represented by a rectangular matrix. A value of 1 is an open cell, and 0 is blocked. Starting at the top-left cell, (0, 0), reach the bottom-right cell, (rows - 1, cols - 1) by moving up, down, left, or right.

Implement solve_rat_maze(maze) using both DFS and BFS. Return the paths found by each algorithm. DFS may return any valid path, while BFS must return a shortest path. Represent each path as an ordered list of [row, column] coordinates. Return an empty list when no path exists.

Use the deterministic neighbor order: down, right, up, left. Both algorithms must avoid revisiting cells.

Formal Specification

  • Input: maze, a non-empty rectangular list of lists containing only 0 and 1.
  • Output: A dictionary with keys dfs and bfs. Each value is a coordinate path, or [] if the destination cannot be reached.
  • The start and destination cells must be included when a path exists.

Constraints

  • 1 <= rows, cols <= 1000
  • The matrix is rectangular
  • maze[r][c] is either 0 or 1
  • Movement is allowed only up, down, left, or right
  • The path may not visit a cell more than once

Function Signature

def solve_rat_maze(maze):
Interviewer

Your question is Rat Maze Traversal. Start with the requirements in the Question tab.

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.
CodePython 3
Sign up free to run your codeLog inLn 2
Run your code to see test output here.