Group movies via graph traversal

Read the full interview experience this question came from →

Quick Overview

This question evaluates graph traversal and connectivity skills, specifically competence with BFS, DFS (iterative and recursive), complexity analysis, and Union-Find for identifying connected components in an undirected graph.

Group movies via graph traversal

Company: Google

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Onsite

You are given n movies labeled 0..n-1 and a list of undirected pairs (a, b) meaning movies a and b are similar. Group all movies into categories where each category is a maximal set of movies connected via similarity links (i.e., connected components). Return the categories as lists of movie IDs, each list sorted, and the collection sorted by the smallest ID in each category. Implement solutions using both BFS and DFS, explain their time/space complexity, and compare iterative vs. recursive DFS. Then discuss an alternative Union-Find approach and when it would be preferable. Dry-run on n=8 with pairs: (0, 1),(1, 2),(3, 4),(5, 6),(6, 7).

Overview: This question evaluates graph traversal and connectivity skills, specifically competence with BFS, DFS (iterative and recursive), complexity analysis, and Union-Find for identifying connected components in an undirected graph.

Read the full Google Software Engineer interview experience this question came from

Community answers

Answer by mojahidislam221

import java.util.*; public class Solution { public List> solution( int n, List> pairs) { List> graph = new ArrayList<>(); for (int i = 0; i < n; i++) { graph.add(new ArrayList<>()); } // Build undirected graph for (List pair : pairs) { int a = pair.get(0); int b = pair.get(1); graph.get(a).add(b); graph.get(b).add(a); } boolean[] visited = new boolean[n]; List> result = new ArrayList<>(); for (int i = 0; i < n; i++) { if (visited[i]) { continue; } List component = new ArrayList<>(); Deque stack = new ArrayDeque<>(); stack.push(i); visited[i] = true; while (!stack.isEmpty()) { int u = stack.pop(); component.add(u); for (int v : graph.get(u)) { if (!visited[v]) { visited[v] = true; stack.push(v); } } } // Sort IDs inside component Collections.sort(component); result.add(component); } // Sort components by their smallest ID result.sort(Comparator.comparingInt(a -> a.get(0))); return result; } }
|Home/Coding & Algorithms/Google
Google logo
Google
Sep 6, 2025
mediumSoftware EngineerOnsiteCoding & Algorithms
12
0

You are given n movies labeled 0..n-1 and a list of undirected pairs (a, b) meaning movies a and b are similar. Group all movies into categories where each category is a maximal set of movies connected via similarity links (i.e., connected components). Return the categories as lists of movie IDs, each list sorted, and the collection sorted by the smallest ID in each category. Implement solutions using both BFS and DFS, explain their time/space complexity, and compare iterative vs. recursive DFS. Then discuss an alternative Union-Find approach and when it would be preferable. Dry-run on n=8 with pairs: (0, 1),(1, 2),(3, 4),(5, 6),(6, 7).

Submit Your Answer to Earn 20XP

Sign in to leave a comment

Loading comments...