KatInterview

Classifying Circle Pair Relationships

Math & Bit ManipulationModel answer
MediumModel answerMath & Bit Manipulationgeometrydistanceedge-cases

Last updated

Determine whether two circles intersect at two points, are tangent at one point, or have no boundary contact.

You're building a graphics layer that needs to understand how circles relate to each other spatially.

Write a function that takes pairs of circles and classifies each pair's relationship. Each circle is defined by its center coordinates and radius: (x1, y1, r1, x2, y2, r2).

Input: A list of tuples, where each tuple contains six numbers representing two circles.

Output: A list of strings in corresponding order. Each string must be one of:

  • "intersect" — the circle boundaries cross at two distinct points
  • "touch" — the circle boundaries meet at exactly one point (externally or internally tangent)
  • "separate" — the circle boundaries never meet (completely disjoint or one entirely inside the other)

Special case: When two circles are identical (same center, same radius), classify as "intersect".

Constraints:

  • Radii are non-negative
  • Coordinates can be integers or floats
  • For floating-point inputs, use appropriate tolerance when checking equality

Example:

Input: [(0, 0, 1, 3, 0, 1), (0, 0, 2, 2, 0, 1), (0, 0, 5, 1, 0, 1)]
Output: ["separate", "intersect", "separate"]

The first pair is too far apart, the second pair crosses at two points, and the third has one circle completely inside the other.

Hints

Hint 1

Calculate the distance between centers and compare it to the sum and difference of the radii

Hint 2

Work with squared distances to avoid square root calculations and improve numerical accuracy

Hint 3

Handle the coincident circle case (same center and radius) separately before checking other conditions