Answer: BOEING Hiring Question | Number of Provinces | On-Campus OA (2021) Number of Pr
Answer · Posted Jun 2026
Approach Initially, every city is its own province. Use Disjoint Set Union (Union-Find) to merge connected cities. Whenever two different provinces are merged, decrease the province count. Return the final province count. Strategy Initialize every city as its own parent. Traverse only the upper triangle of the matrix. If two cities are connected, perform a union operation. Count remaining connected components. Java Code class Solution { int[] parent; public int findCircleNum(int[][] isConnected) { int n = isConnected.length; parent = new ...
The full answer & interview discussion are available to premium members.
Log in Create a free account