MEDIUMasked at 6 companies

Number of Provinces

A medium-tier problem at 69% community acceptance, tagged with Depth-First Search, Breadth-First Search, Union Find. Reported in interviews at Two Sigma and 5 others.

Founder's read

Number of Provinces is a connected-components problem that hits screens at Amazon, Google, and Uber. It looks simple on the surface: count distinct groups in a 2D adjacency matrix. But the trap is implementation. Most candidates write DFS or BFS correctly in isolation, then botch the state tracking or graph representation. The acceptance rate hovers around 69%, which means one in three people either time out, visit nodes twice, or misread the matrix format. If this lands in your live assessment and you freeze on the graph setup, StealthCoder surfaces a clean solution in seconds, invisible to the proctor.

Companies asking
6
Difficulty
MEDIUM
Acceptance
69%

Companies that ask "Number of Provinces"

If this hits your live OA

Number of Provinces is the kind of problem that decides whether you pass. StealthCoder reads the problem on screen and surfaces a working solution in under 2 seconds. Invisible to screen share. The proctor sees nothing. Built by a senior engineer who knows the OA is theater. This is the script.

Get StealthCoder
What this means

The problem asks you to find connected components in an undirected graph represented as an adjacency matrix, where each row i contains 1s at indices j if provinces i and j are directly connected. The gotcha: candidates often treat this like a general graph-traversal problem and miss that you're not building an adjacency list first. You can iterate rows on the fly, mark visited nodes with a boolean array, and trigger DFS or BFS from any unvisited node. The real pitfall is forgetting that the matrix is symmetric and that a single dfs() call from an unvisited node will flood-fill its entire component, so you only increment the province counter once per component. Union Find is cleaner for many and avoids the graph representation entirely, but it requires a union by rank or path compression to stay efficient. When the OA clock is running and you blank on the iteration order or whether to use adjacency-matrix traversal versus Union Find, StealthCoder runs invisibly and gives you a tested, minimal implementation.

Pattern tags

The honest play

You know the problem. Make sure you actually pass it.

Number of Provinces recycles across companies for a reason. It's medium-tier, and most candidates blank under the timer. StealthCoder is the hedge: an AI overlay invisible during screen share. It reads the problem and surfaces a working solution in under 2 seconds. Built by a senior engineer who knows the OA is theater. This is the script. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Number of Provinces interview FAQ

Is this really a medium problem or is it just easy with a scary name?+

It's a medium. The concepts (DFS, BFS, Union Find) are easy, but the adjacency matrix representation trips people up. You must track visited state correctly and understand that one DFS call covers an entire component. The 69% acceptance rate reflects those mistakes, not the core difficulty.

Should I use DFS, BFS, or Union Find?+

All three work. DFS is fastest to code and least error-prone. BFS requires a queue but is equally valid. Union Find is the most elegant and scales well if you use rank-based unions. Pick the one you can code fastest without bugs in a live setting.

Do Google and Amazon actually ask this or is it just historical data?+

Both are in the reported company list. It's a standard connected-components problem, so expect it at large tech firms. It's not a trick question, just a medium-difficulty graph fundamentals check.

What's the most common mistake people make?+

Forgetting to mark nodes as visited before exploring their neighbors, which causes infinite loops or double-counting. The second mistake is treating each edge separately instead of realizing one DFS call covers the entire connected component.

Can I solve this without building an explicit adjacency list?+

Yes. You can iterate the matrix directly inside your DFS or BFS. Check matrix[node][i] == 1 to find neighbors, and use a visited array to avoid revisiting. This saves memory and is faster than pre-building a list.

Want the actual problem statement? View "Number of Provinces" on LeetCode →

Frequency and company-tag data sourced from public community-maintained interview-report repos. Problem, description, and trademark © LeetCode. StealthCoder is not affiliated with LeetCode.