Count Friend Circles
Reported by candidates from OpenAI's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The OpenAI OA reported in October 2026 hides its trap in a tiny detail: friendship is transitive through chains, so checking direct neighbors alone gives the wrong count. If A knows B and B knows C, that's one circle, even when A and C never connect. This is Count Friend Circles on an n x n adjacency matrix, a connected components problem. It looks easy, and that's why people rush it and miss the indirect links. If you blank mid-assessment, StealthCoder runs invisibly on your desktop and gives you a working solution in real time. Know the pattern first and you probably won't need it.
The problem
You are given an n x n matrix isConnected. If isConnected[i][j] = 1, person i and person j are direct friends. A friend circle is a group of directly or indirectly connected people with no friendship to anyone outside the group. Examples Example 1 isConnected = [[1,1,0],[1,1,0],[0,0,1]] return = 2 People 0 and 1 form one friend circle, while person 2 forms another.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is to count connected components. Loop over every person. If they haven't been visited, increment the circle count and run DFS or BFS from them, marking everyone reachable through the matrix row. Each person gets visited once, so you do O(n^2) work reading the matrix. The naive mistake is counting rows that have a 1 off the diagonal, or counting pairs, which ignores indirect friendship. Another slip is forgetting that the diagonal is always 1, so a person alone still forms a circle. Union-Find works equally well: union every pair where the cell is 1, then count distinct roots. Pick whichever you can write without bugs under pressure. If your mind goes blank on the live OA, StealthCoder is the hedge, since it reads the problem and hands you the traversal while the proctor sees nothing.
Memorize the pattern. If you can't, run StealthCoder. The proctor sees the IDE. They don't see what's behind it.
You can drill Count Friend Circles cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge.
Get StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as number of provinces. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass OpenAI's OA.
OpenAI reuses patterns across OAs. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Count Friend Circles FAQ
What's the trick to Count Friend Circles?+
Treat the matrix as a graph and count connected components. For each unvisited person, start a DFS or BFS, mark everyone reachable, and add one to your count. Indirect friendships are covered automatically because the traversal follows chains of connections.
How hard is this OpenAI question really?+
It's medium at most. The logic is short, but the difficulty is realizing that indirect connections matter. If you know graph traversal or Union-Find, you can finish it quickly. Most failures come from counting direct neighbors instead of whole components.
Should I use DFS, BFS, or Union-Find?+
Any of them passes. DFS is the shortest to write recursively. BFS avoids recursion depth worries if n is large. Union-Find is clean if you already have a template memorized. Choose the one you can write bug-free from memory.
What edge cases break a naive solution?+
A person with no friends except themselves still counts as a circle, since the diagonal is 1. A chain like 0-1, 1-2 with no 0-2 link must count as one circle. Also test n equal to 1 and a fully connected matrix.
How do I prepare for this in 48 hours?+
Write connected components on an adjacency matrix twice from scratch, once with DFS and once with Union-Find. Test it on a chain, an all-isolated matrix, and a full matrix. That covers this pattern, and it shows up in many graph questions.