PracHub
QuestionsCoachesLearningGuidesInterview Prep
|Home/Coding & Algorithms/Google

Group movies via graph traversal

Last updated: Mar 29, 2026

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.

  • medium
  • Google
  • Coding & Algorithms
  • Software Engineer

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.

Related Interview Questions

  • Find A Threshold-Limited Path With Minimum Required Safety - Google (medium)
  • Build Prefix Lookup with a Trie - Google (medium)
  • Deterministic Task Execution Order - Google (easy)
  • Busiest Rental Car - Google (easy)
  • Find Common Free Time Slots Across Calendars - Google (easy)
|Home/Coding & Algorithms/Google

Group movies via graph traversal

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

Browse More Questions

More Coding & Algorithms•More Google•More Software Engineer•Google Software Engineer•Google Coding & Algorithms•Software Engineer Coding & Algorithms
PracHub

Master your tech interviews with 8,500+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • AI Coding Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.