PracHub
QuestionsPremiumLearningGuidesCheatsheetNEWCoaches
|Home/Data Manipulation (SQL/Python)/Microsoft

Find common friends from directed edges

Last updated: Mar 29, 2026

Quick Overview

This question evaluates a candidate's ability to manipulate relational data and reason about graph reciprocity, specifically testing SQL query formulation, aggregation, deduplication, and indexing for performance in the Data Manipulation (SQL/Python) domain.

  • Medium
  • Microsoft
  • Data Manipulation (SQL/Python)
  • Data Scientist

Find common friends from directed edges

Company: Microsoft

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: Medium

Interview Round: Technical Screen

You have a directed edge list that records who followed whom. A mutual “friendship” exists only if both directions appear (A→B and B→A). Schema and sample data: Schema: FriendEdges(id INT PRIMARY KEY, user_from VARCHAR(10), user_to VARCHAR(10)) Sample rows: id | user_from | user_to 1 | A | B 2 | B | A 3 | A | C 4 | C | A 5 | A | D 6 | D | A 7 | B | C 8 | C | B Tasks: 1) Write a single SQL query that derives an undirected friendship table Friends(u1, u2) containing one row per friendship with u1 < u2, based on reciprocal edges in FriendEdges. 2) Using only FriendEdges (no temporary tables), write a single SQL query that returns, for every unordered pair of distinct users (x, y) with x < y, all of their common friends f (users who are friends with both x and y). Output columns: user1, user2, common_friend. Exclude the pair themselves from being counted as their own friend. 3) Extend (2) to also return, per pair (x, y), the count of distinct common friends. Ensure no duplicates even if multiple reciprocal edges are present. 4) Explain the indexes you would add on FriendEdges to make (2) performant on 100M rows, and why.

Quick Answer: This question evaluates a candidate's ability to manipulate relational data and reason about graph reciprocity, specifically testing SQL query formulation, aggregation, deduplication, and indexing for performance in the Data Manipulation (SQL/Python) domain.

Related Interview Questions

  • Query departments and top earners - Microsoft (easy)
  • Query email logs for deliverability insights - Microsoft (Medium)
  • Compute most popular location with weights - Microsoft (Medium)
  • Count words in a document robustly - Microsoft (Medium)
Microsoft logo
Microsoft
Oct 13, 2025, 9:49 PM
Data Scientist
Technical Screen
Data Manipulation (SQL/Python)
5
0

You have a directed edge list that records who followed whom. A mutual “friendship” exists only if both directions appear (A→B and B→A). Schema and sample data:

Schema: FriendEdges(id INT PRIMARY KEY, user_from VARCHAR(10), user_to VARCHAR(10))

Sample rows: id | user_from | user_to 1 | A | B 2 | B | A 3 | A | C 4 | C | A 5 | A | D 6 | D | A 7 | B | C 8 | C | B

Tasks:

  1. Write a single SQL query that derives an undirected friendship table Friends(u1, u2) containing one row per friendship with u1 < u2, based on reciprocal edges in FriendEdges.
  2. Using only FriendEdges (no temporary tables), write a single SQL query that returns, for every unordered pair of distinct users (x, y) with x < y, all of their common friends f (users who are friends with both x and y). Output columns: user1, user2, common_friend. Exclude the pair themselves from being counted as their own friend.
  3. Extend (2) to also return, per pair (x, y), the count of distinct common friends. Ensure no duplicates even if multiple reciprocal edges are present.
  4. Explain the indexes you would add on FriendEdges to make (2) performant on 100M rows, and why.

Comments (0)

Sign in to leave a comment

Loading comments...

Browse More Questions

More Data Manipulation (SQL/Python)•More Microsoft•More Data Scientist•Microsoft Data Scientist•Microsoft Data Manipulation (SQL/Python)•Data Scientist Data Manipulation (SQL/Python)
PracHub

Master your tech interviews with 7,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
  • Compare Platforms
  • Discord Community

Support

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

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.