← Databricks Interview Insights

Databricks·Data Scientist·Technical Phone Screen·Intermediate

Intermediate
Jun 2026

Summary

SQL question for a Data Scientist role at Databricks, graph-style problem dressed up as a simple following table. One question, pretty focused, no fluff.

Questions Asked (1)

Q1

Given a table of YouTuber-follower relationships, write a SQL query that returns the count of distinct second-degree followers for each YouTuber, excluding anyone who already follows them directly and excluding the YouTuber themselves.

Data ModelingAlgorithms & Data Structures
Author's notes

I knew immediately it was a self-join situation but tripped up on the exclusion logic.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

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.

1. Understand the schema and definitions

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.

2. Self-join to find second-degree paths

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.

3. Exclude direct followers and 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.

4. Count distinct second-degree followers

Group by the YouTuber and count distinct f2.follower_id to avoid duplicates from multiple paths.

5. Write and validate the query

Assemble the SQL, test with sample data, and consider edge cases like cycles or self-follows.

Key Points to Mention

  • Self-join technique to traverse relationships
  • Use of DISTINCT to avoid counting duplicates
  • Filtering out direct followers with NOT EXISTS or LEFT JOIN
  • Excluding the YouTuber themselves
  • Handling potential cycles or self-follows
  • Performance considerations for large datasets (indexes, query optimization)

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.