← Databricks Interview Insights
I knew immediately it was a self-join situation but tripped up on the exclusion logic.
Use a self-join on the followers table to find second-degree connections: join the table to itself where the first user's follower is the second user's followee. Then filter out direct follows and the YouTuber themselves, and count distinct second-degree followers per YouTuber.
Pro tip: Clarify the definition of 'second-degree follower' upfront—it's a follower of a follower, not just any indirect connection. Also, consider performance: use DISTINCT and appropriate indexes to handle large datasets.
Identify the table structure (e.g., columns like follower_id, followee_id) and clarify what 'second-degree follower' means: a user who follows someone who follows the YouTuber.
Join the table to itself: first join (f1) where f1.followee_id = YouTuber, then join (f2) where f2.followee_id = f1.follower_id. This gives users who follow a follower of the YouTuber.
Use NOT EXISTS or LEFT JOIN with NULL check to remove users who already follow the YouTuber directly, and add a condition to exclude the YouTuber themselves.
Group by the YouTuber and count distinct f2.follower_id to avoid duplicates from multiple paths.
Assemble the SQL, test with sample data, and consider edge cases like cycles or self-follows.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.