Answer: HashiCorp Associate Developer Interview Question 2023 August | Graphs and Union
Answer · Posted Jun 2026
Solution: DFS to Count Connected Components Approach This is a classic connected components problem on an undirected graph represented as an adjacency matrix. Iterate through all cities. For each unvisited city, launch a DFS to visit all cities reachable from it — marking them all as visited. Each DFS launch corresponds to discovering a new province. Count how many times DFS is launched — that is the number of provinces. Algorithm Walkthrough Create a visited[n] boolean array, initialize all to ...
The full answer & interview discussion are available to premium members.
Log in Create a free account