Skip to main content
All Questions

Count the friend circles among n people given direct friendship pairs.

Unlock detailed company stats for this questionUpgrade

You are given a list of n people labeled 0 to n-1. Some are directly friends, given as pairs in friendships. A friend circle is a group of people who are directly or indirectly friends. Count the friend circles.

Example: n = 5, friendships = [[0, 1], [2, 3], [3, 4]]. Output: 2, for the circles {0,1} and {2,3,4}.

The interviewer asked for time and space complexity. Follow-up: count the redundant edges, meaning connections that can be removed without changing the number of friend circles.

Expect follow-ups like

  • Count the redundant edges: connections that can be removed without affecting the number of friend circles.

Community answers

No answers contributed by the community yet.

Related courses

Course

Software Engineering Interview Prep

Land your dream software engineering role at Google, Amazon, Microsoft, Meta, Apple, and other top companies. Learn from mock interviews, frameworks, and advice from senior candidates—practice data structures, algorithms, system design, people management, behavioral interviews, and more.

Course

System Design Interviews

Learn how to answer the latest system design questions across product, infrastructure, and AI domains. Features in-depth video examples, written breakdowns, and helpful reference patterns. Watch senior engineers and managers answer these questions in mock interview videos.