Stealth Interview
  • Features
  • Pricing
  • Blog
  • Login
  • Sign up

Leetcode #1036: Escape a Large Maze

In this guide, we solve Leetcode #1036 Escape a Large Maze in Python and focus on the core idea that makes the solution efficient.

You will see the intuition, the step-by-step method, and a clean Python implementation you can use in interviews.

Leetcode

Problem Statement

There is a 1 million by 1 million grid on an XY-plane, and the coordinates of each grid square are (x, y). We start at the source = [sx, sy] square and want to reach the target = [tx, ty] square.

Quick Facts

  • Difficulty: Hard
  • Premium: No
  • Tags: Depth-First Search, Breadth-First Search, Array, Hash Table

Intuition

Fast membership checks and value lookups are the heart of this problem, which makes a hash map the natural choice.

By storing what we have already seen (or counts/indexes), we can answer the question in one pass without backtracking.

Approach

Scan the input once, using the map to detect when the condition is satisfied and to update state as you go.

This keeps the solution linear while remaining easy to explain in an interview setting.

Steps:

  • Initialize a hash map for seen items or counts.
  • Iterate through the input, querying/updating the map.
  • Return the first valid result or the final computed value.

Example

Input: blocked = [[0,1],[1,0]], source = [0,0], target = [0,2] Output: false Explanation: The target square is inaccessible starting from the source square because we cannot move. We cannot move north or east because those squares are blocked. We cannot move south or west because we cannot go outside of the grid.

Python Solution

class Solution: def isEscapePossible( self, blocked: List[List[int]], source: List[int], target: List[int] ) -> bool: def dfs(source: List[int], target: List[int], vis: set) -> bool: vis.add(tuple(source)) if len(vis) > m: return True for a, b in pairwise(dirs): x, y = source[0] + a, source[1] + b if 0 <= x < n and 0 <= y < n and (x, y) not in s and (x, y) not in vis: if [x, y] == target or dfs([x, y], target, vis): return True return False s = {(x, y) for x, y in blocked} dirs = (-1, 0, 1, 0, -1) n = 10**6 m = len(blocked) ** 2 // 2 return dfs(source, target, set()) and dfs(target, source, set())

Complexity

The time complexity is O(n). The space complexity is O(n).

Edge Cases and Pitfalls

Watch for boundary values, empty inputs, and duplicate values where applicable. If the problem involves ordering or constraints, confirm the invariant is preserved at every step.

Summary

This Python solution focuses on the essential structure of the problem and keeps the implementation interview-friendly while meeting the constraints.


Ace your next coding interview

We're here to help you ace your next coding interview.

Subscribe
Stealth Interview
© 2026 Stealth Interview®Stealth Interview is a registered trademark. All rights reserved.
Product
  • Blog
  • Pricing
Company
  • Terms of Service
  • Privacy Policy