Group movies via graph traversal

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).

Quick Answer: 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.

|Home/Coding & Algorithms/Google
Google logo
Google
Sep 6, 2025, 12:00 AM
mediumSoftware EngineerOnsiteCoding & Algorithms
9
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...