Classify two circles as concentric, touching, intersecting or disjoint

Quick Overview

Given the integer centers and radii of two circles, return whether they are concentric, touching, intersecting or disjoint. It tests careful case analysis on the center distance and the radii, exact comparisons without floating point, and edge cases such as internal tangency and one circle nested inside another.

Classify two circles as concentric, touching, intersecting or disjoint

Company: IBM

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

Given the center coordinates and radius of two circles, determine their spatial relationship. The first circle has center `(x1, y1)` and radius `r1`. The second circle has center `(x2, y2)` and radius `r2`. Return one of the following strings based on the relationship between the two circles: - `"concentric"`: the circles share the same center (regardless of whether they have the same radius). - `"touching"`: the circles touch at exactly one point (either externally or internally). - `"intersecting"`: the circles overlap and intersect at two points. - `"disjoint"`: the circles do not touch or overlap (either one is completely inside the other without touching, or they are completely separate). All inputs are integers, so comparisons can be done exactly without floating point precision issues. ### Function Signature ```python def circle_relationship(x1: int, y1: int, r1: int, x2: int, y2: int, r2: int) -> str: ``` ### Rules - Return exactly one of the four lowercase strings listed above. - `"concentric"` takes precedence over every other case. If the centers coincide, return `"concentric"` even when one circle lies inside the other or the two circles are identical. - `"touching"` covers external tangency (each circle lies outside the other and they meet at one point) and internal tangency (one circle lies inside the other and meets it at one point). ### Constraints - `-10⁴ <= x1, y1, x2, y2 <= 10⁴` - `1 <= r1, r2 <= 10⁴` ### Examples ```text Input: x1 = 0, y1 = 0, r1 = 5, x2 = 0, y2 = 0, r2 = 3 Output: "concentric" ``` Both circles share the center (0, 0) but have different radii (5 and 3), so they are concentric. ```text Input: x1 = 0, y1 = 0, r1 = 3, x2 = 5, y2 = 0, r2 = 2 Output: "touching" ``` The distance between the centers is 5, and the sum of the radii is `3 + 2 = 5`. Since the distance equals the sum, the circles touch externally at the single point (3, 0). ```text Input: x1 = 0, y1 = 0, r1 = 5, x2 = 1, y2 = 0, r2 = 2 Output: "disjoint" ``` The centers are 1 apart, which is less than the difference of the radii, `5 - 2 = 3`. Every point of the second circle is at most `1 + 2 = 3` from the origin, so it sits strictly inside the first circle and never reaches its boundary.

Overview: Given the integer centers and radii of two circles, return whether they are concentric, touching, intersecting or disjoint. It tests careful case analysis on the center distance and the radii, exact comparisons without floating point, and edge cases such as internal tangency and one circle nested inside another.

IBM logo
IBM
Nov 24, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

Given the center coordinates and radius of two circles, determine their spatial relationship.

The first circle has center (x1, y1) and radius r1. The second circle has center (x2, y2) and radius r2.

Return one of the following strings based on the relationship between the two circles:

  • "concentric" : the circles share the same center (regardless of whether they have the same radius).
  • "touching" : the circles touch at exactly one point (either externally or internally).
  • "intersecting" : the circles overlap and intersect at two points.
  • "disjoint" : the circles do not touch or overlap (either one is completely inside the other without touching, or they are completely separate).

All inputs are integers, so comparisons can be done exactly without floating point precision issues.

Function Signature

def circle_relationship(x1: int, y1: int, r1: int, x2: int, y2: int, r2: int) -> str:

Rules

  • Return exactly one of the four lowercase strings listed above.
  • "concentric" takes precedence over every other case. If the centers coincide, return "concentric" even when one circle lies inside the other or the two circles are identical.
  • "touching" covers external tangency (each circle lies outside the other and they meet at one point) and internal tangency (one circle lies inside the other and meets it at one point).

Constraints

  • -10⁴ <= x1, y1, x2, y2 <= 10⁴
  • 1 <= r1, r2 <= 10⁴

Examples

Input: x1 = 0, y1 = 0, r1 = 5, x2 = 0, y2 = 0, r2 = 3
Output: "concentric"

Both circles share the center (0, 0) but have different radii (5 and 3), so they are concentric.

Input: x1 = 0, y1 = 0, r1 = 3, x2 = 5, y2 = 0, r2 = 2
Output: "touching"

The distance between the centers is 5, and the sum of the radii is 3 + 2 = 5. Since the distance equals the sum, the circles touch externally at the single point (3, 0).

Input: x1 = 0, y1 = 0, r1 = 5, x2 = 1, y2 = 0, r2 = 2
Output: "disjoint"

The centers are 1 apart, which is less than the difference of the radii, 5 - 2 = 3. Every point of the second circle is at most 1 + 2 = 3 from the origin, so it sits strictly inside the first circle and never reaches its boundary.