Skip to main content

Check if the Rectangle Corner Is Reachable - Solution & Explanation

HardArrayMathDepth-First SearchBreadth-First Search42 min readAsked at: Amazon, Google
Practice this problem

Problem Statement

You are given two positive integers xCorner and yCorner, and a 2D array circles, where circles[i] = [xi, yi, ri] denotes a circle with center at (xi, yi) and radius ri.

There is a rectangle in the coordinate plane with its bottom left corner at the origin and top right corner at the coordinate (xCorner, yCorner). You need to check whether there is a path from the bottom left corner to the top right corner such that the entire path lies inside the rectangle, does not touch or lie inside any circle, and touches the rectangle only at the two corners.

Return true if such a path exists, and false otherwise.

 

Example 1:

Input: xCorner = 3, yCorner = 4, circles = [[2,1,1]]

Output: true

Explanation:

The black curve shows a possible path between (0, 0) and (3, 4).

Example 2:

Input: xCorner = 3, yCorner = 3, circles = [[1,1,2]]

Output: false

Explanation:

No path exists from (0, 0) to (3, 3).

Example 3:

Input: xCorner = 3, yCorner = 3, circles = [[2,1,1],[1,2,1]]

Output: false

Explanation:

No path exists from (0, 0) to (3, 3).

Example 4:

Input: xCorner = 4, yCorner = 4, circles = [[5,5,1]]

Output: true

Explanation:

 

Constraints:

  • 3 <= xCorner, yCorner <= 109
  • 1 <= circles.length <= 1000
  • circles[i].length == 3
  • 1 <= xi, yi, ri <= 109

Approach Overview

Problem Overview: You are given a rectangle and several circular obstacles. The task is to determine whether you can move from the bottom-left corner to the top-right corner without entering any circle. Movement is allowed inside the rectangle, but touching or crossing a circle blocks the path.

Approach 1: Grid-Based BFS Traversal (Time: O(W Ɨ H), Space: O(W Ɨ H))

This approach converts the rectangle into a grid and performs a Breadth-First Search. Each grid cell represents a coordinate in the rectangle. First mark all cells that fall inside any circle using a distance check: (x - cx)^2 + (y - cy)^2 ≤ r^2. These cells become blocked. Then start BFS from (0,0), exploring valid neighbors while avoiding blocked cells and staying inside bounds. If BFS reaches the top-right corner, the path exists.

The key idea is modeling the geometry problem as a graph traversal problem. BFS guarantees that all reachable cells are explored systematically. This approach is easy to implement and intuitive when thinking in terms of reachable states, but it becomes expensive when the rectangle dimensions are large.

Approach 2: Mathematical Distance + Union-Find (Time: O(n²), Space: O(n))

The optimized approach avoids scanning the entire grid. Instead, treat each circle as a node and detect whether circles connect to form a continuous barrier that blocks the path. Two circles are connected if the distance between their centers is less than or equal to the sum of their radii. Use Union Find (Disjoint Set Union) to group overlapping circles.

Next check whether any connected component touches both sides of the rectangle in a way that forms a wall (for example, connecting the left and right edges or top and bottom edges, depending on constraints). If such a barrier exists, it prevents reaching the opposite corner. Distance checks and boundary intersection tests rely on simple geometry formulas. This method scales well because it only processes circle relationships rather than every grid point.

Recommended for interviews: Start by explaining the grid BFS approach because it clearly models the reachable-area idea. Then move to the mathematical union-find strategy. Interviewers usually expect the optimized geometric reasoning since it reduces the search space dramatically and demonstrates strong problem-solving with geometry and graph connectivity.

Approach 1: Grid-Based BFS Approach

This approach involves treating the rectangle as a grid and using a Breadth-First Search (BFS) algorithm to find a path from the bottom left to the top right corner. The key challenge is marking areas that are obstructed by circles as inaccessible. For high efficiency, we ensure that only necessary points inside the rectangle are checked and circular areas are systematically marked as blocked by checking their distance to any circle's center.

This C program treats the grid within the rectangle as explored through BFS. It checks if points are inside any circle using Euclidean distance, blocking those that are.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(xCorner * yCorner * circlesSize), Space Complexity: O(xCorner * yCorner)

Try this approach in the editor →

Approach 2: Mathematical Distance Calculations

This approach calculates distances mathematically to determine if traversal from the start to the end is possible by potentially taking straight paths while checking if these intersect with any circles. By leveraging geometric properties directly, one can avoid unnecessary grid computation and reduce complexity through direct distance evaluations and thorough conditional logic.

A C solution using Euclidean distance between points, checking if any circle center within the rectangle obstructs start-to-end pathways directly by examining radial intersections.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(circlesSize), Space Complexity: O(1)

Try this approach in the editor →

Approach 3: DFS + Mathematics

According to the problem description, we discuss the following cases:

When there is only one circle in circles:

  1. If the starting point (0, 0) is inside the circle (including the boundary), or the ending point (xCorner, yCorner) is inside the circle, then it is impossible to satisfy the condition of "not touching the circle".
  2. If the circle intersects with the left or top side of the rectangle and also intersects with the right or bottom side of the rectangle, then the circle will block the path from the bottom-left corner to the top-right corner of the rectangle, making it impossible to satisfy the condition of "not touching the circle".

When there are multiple circles in circles:

  1. Similar to the above case, if the starting point or ending point is inside a circle, it is impossible to satisfy the condition of "not touching the circle".
  2. If there are multiple circles, they may intersect within the rectangle, forming a larger obstacle area. As long as this obstacle area intersects with the left or top side of the rectangle and also intersects with the right or bottom side of the rectangle, it is impossible to satisfy the condition of "not touching the circle". If the intersecting area is not inside the rectangle, it cannot be merged because the intersecting area cannot block the path inside the rectangle. Additionally, if part of the intersecting area is inside the rectangle and part is outside, these circles can be used as starting or ending points and can be merged or not. We only need to choose one of the intersecting points. If this point is inside the rectangle, we can merge these circles.

Based on the above analysis, we traverse all circles. For the current circle, if the starting point or ending point is inside the circle, we directly return false. Otherwise, if this point has not been visited and the circle intersects with the left or top side of the rectangle, we start a depth-first search (DFS) from this circle. During the search, if we find a circle that intersects with the right or bottom side of the rectangle, it means the obstacle area formed by the circles blocks the path from the bottom-left corner to the top-right corner of the rectangle, and we return false.

We define dfs(i) to represent starting a DFS from the i-th circle. If we find a circle that intersects with the right or bottom side of the rectangle, we return true; otherwise, we return false.

The execution process of the function dfs(i) is as follows:

  1. If the current circle intersects with the right or bottom side of the rectangle, return true;
  2. Otherwise, mark the current circle as visited;
  3. Next, traverse all other circles. If circle j has not been visited, and circle i intersects with circle j, and one of the intersection points of these two circles is inside the rectangle, continue the DFS from circle j. If we find a circle that intersects with the right or bottom side of the rectangle, return true;
  4. If no such circle is found, return false.

In the above process, we need to determine whether two circles O_1 = (x_1, y_1, r_1) and O_2 = (x_2, y_2, r_2) intersect. If the distance between the centers of the two circles does not exceed the sum of their radii, i.e., (x_1 - x_2)^2 + (y_1 - y_2)^2 \le (r_1 + r_2)^2, then they intersect.

We also need to find an intersection point of the two circles. We take a point A = (x, y) such that \frac{O_1 A}{O_1 O_2} = \frac{r_1}{r_1 + r_2}. If the two circles intersect, point A must be in the intersection. In this case, \frac{x - x_1}{x_2 - x_1} = \frac{r_1}{r_1 + r_2}, solving for x = \frac{x_1 r_2 + x_2 r_1}{r_1 + r_2}. Similarly, y = \frac{y_1 r_2 + y_2 r_1}{r_1 + r_2}. As long as this point is inside the rectangle, we can continue the DFS, satisfying:

$ \begin{cases} x_1 r_2 + x_2 r_1 < (r_1 + r_2) times xCorner \ y_1 r_2 + y_2 r_1 < (r_1 + r_2) times yCorner \end{cases}

The time complexity is O(n^2), and the space complexity is O(n). Here, n$ is the number of circles.

Code

Python

Java

C++

Go

TypeScript

Rust

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
Grid-Based BFS Approach

Time Complexity: O(xCorner * yCorner * circlesSize), Space Complexity: O(xCorner * yCorner)

Mathematical Distance Calculations

Time Complexity: O(circlesSize), Space Complexity: O(1)

DFS + Mathematics—

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Grid-Based BFS TraversalO(W Ɨ H)O(W Ɨ H)Useful for small rectangle sizes or when modeling the problem as a reachable grid is easier to reason about.
Mathematical Distance + Union-FindO(n²)O(n)Best for large coordinate ranges where scanning the entire grid is infeasible. Focuses only on circle interactions.

Video Solution

A-D | Leetcode Weekly Contest 408 Editorials | Check if the Rectangle Corner Is Reachable | Solution • Abhinav Awasthi • 7,005 views views

Watch 4 more video solutions →

Frequently Asked Questions

Is Check if the Rectangle Corner Is Reachable easy or hard?
Check if the Rectangle Corner Is Reachable is classified as a Hard problem. It combines geometry, graph traversal, and connectivity reasoning, making it challenging without recognizing the union-find barrier concept.
Check if the Rectangle Corner Is Reachable Python/Java solution
The solution can be implemented in Python, Java, C++, C#, or JavaScript. The BFS method uses a queue to explore reachable grid cells, while the optimized solution uses arrays for Union-Find and distance formulas to detect circle overlaps.
How to solve Check if the Rectangle Corner Is Reachable in O(n^2)?
Use a mathematical approach that checks pairwise circle overlap. If the distance between two circle centers is less than or equal to the sum of their radii, union them in a Disjoint Set. Then verify whether any connected component touches rectangle boundaries in a way that blocks the path from start to destination.
What is the best approach for Check if the Rectangle Corner Is Reachable?
The most efficient solution uses geometric distance checks combined with Union-Find. Circles are treated as nodes, and overlapping circles are grouped into connected components. If a component forms a barrier touching critical rectangle boundaries, it blocks the path. This approach runs in O(n^2) time and O(n) space.
Is Check if the Rectangle Corner Is Reachable asked at Google/Amazon/Meta?
Geometry and graph connectivity problems like this frequently appear in interviews at companies such as Google, Amazon, and Meta. Variants that combine BFS, Union-Find, and geometric constraints are especially common in system-level algorithm interviews.
What data structure is used in Check if the Rectangle Corner Is Reachable?
Common data structures include queues for BFS traversal and Disjoint Set Union (Union-Find) for grouping overlapping circles. The optimized approach mainly relies on Union-Find along with mathematical distance calculations.
What is the time complexity of Check if the Rectangle Corner Is Reachable?
The grid BFS approach takes O(W Ɨ H) time and space because every grid cell may be visited. The optimized geometric solution compares circle pairs and runs in O(n^2) time with O(n) space using Union-Find.

Ready to solve this problem?

Practice Check if the Rectangle Corner Is Reachable with our built-in code editor and test cases.

Practice on FleetCode