Article start
DSA Interview Patterns Roadmap
Company Asked Variants

Circle Group Connectivity

Turn the Circle Group Connectivity interview variant into a clear brute-force baseline, optimized pattern, and implementation plan.

May 29, 2026·20

Learning Outcome

Turn the Circle Group Connectivity interview variant into a clear brute-force baseline, optimized pattern, and implementation plan.

Original Interview Statement

Given circles, decide whether they belong to one connected group through overlaps/tangencies.

Examples

ItemDetail
circles touching in a chainconnected

Brute Force Approach

Run DFS from each circle checking all others repeatedly without compression.

Optimized Approach

Use DSU. Two circles connect if center distance is at most r1+r2 and at least abs(r1-r2) if strict boundary-only grouping is required by the variant.

Exact Pseudocode

for every pair of circles:
  if overlap/touch: union(i,j)
answer = number of DSU roots

Reference Code

def circle_groups(circles):
    n = len(circles)
    parent = list(range(n))
    def find(x):
        while parent[x] != x:
            parent[x] = parent[parent[x]]
            x = parent[x]
        return x
    def union(a, b):
        ra, rb = find(a), find(b)
        if ra != rb:
            parent[rb] = ra
    for i in range(n):
        x1, y1, r1 = circles[i]
        for j in range(i + 1, n):
            x2, y2, r2 = circles[j]
            d2 = (x1 - x2) ** 2 + (y1 - y2) ** 2
            if d2 <= (r1 + r2) ** 2:
                union(i, j)
    return len({find(i) for i in range(n)})

Complexity

ItemDetail
Brute forceO(n^3) repeated DFS variants
OptimizedO(n^2 alpha(n))

Edge Cases

  • Nested circles
  • Tangency exactly
  • Disconnected groups

Follow-ups

  • Can a path cross from source to target?
  • Block rectangle with circles

Nearest Practice References

  • Number of Provinces
  • Circle overlap DSU

Common Mistakes

  • Copying the nearest LeetCode solution without checking the changed rule.
  • Skipping duplicate or boundary cases.
  • Not stating the brute force before the optimized approach.

Discussion

0 comments

Sign in to share a question or add to the discussion.
Start the discussion

Ask a question or share what stood out to you.