Ctrl K

Circle Meets Rectangle

Medium
A plane holds a circle centered at (cx, cy) with radius r, and an axis-aligned rectangle whose bottom-left corner is (left, bottom) and top-right corner is (right, top). Both shapes are filled and include their boundary. Return True if they share at least one point, and False otherwise.
Example 1
r=2, cx=0, cy=0, left=2, bottom=-1, right=5, top=4 → True

The circle touches the rectangle's left side at (2, 0), so they share a point.

Example 2
r=2, cx=0, cy=0, left=2, bottom=2, right=6, top=5 → False

The closest point of the rectangle is the corner (2, 2), which is 2√2 ≈ 2.83 > 2 away.

Example 3
r=1, cx=3, cy=3, left=0, bottom=0, right=10, top=10 → True

The whole disk lies inside the rectangle.

Constraints
  • 1 ≤ r ≤ 2000
  • -10⁴ ≤ cx, cy ≤ 10⁴
  • -10⁴ ≤ left < right ≤ 10⁴
  • -10⁴ ≤ bottom < top ≤ 10⁴
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsArea Under Two Rectangles MediumNo Inward Corners MediumFour Pins Make a Square MediumLaser in a Mirror Room MediumSmallest Paddock Medium
loading Python…
Input
r=2, cx=0, cy=0, left=2, bottom=-1, right=5, top=4
Expected
True

Run checks the first 3 tests in your browser. Submit checks all 109 tests on the server, hidden ones included.