Friends With No Mutual Friends - Problem
Friends With No Mutual Friends

You are given a Friends table representing a social network where friendships are bidirectional. Each row contains two user IDs who are friends with each other.

Your task is to find all pairs of users who are direct friends but have zero mutual friends in common. In other words, find friend pairs where no other user is friends with both of them.

Table Schema:
Friends
+-------------+------+
| Column Name | Type |
+-------------+------+
| user_id1 | int |
| user_id2 | int |
+-------------+------+

The combination (user_id1, user_id2) is the primary key. Return the result ordered by user_id1, user_id2 in ascending order.

Input & Output

example_1.sql — Basic Case
$ Input: Friends table: +----------+----------+ | user_id1 | user_id2 | +----------+----------+ | 1 | 2 | | 1 | 3 | | 2 | 3 | | 4 | 5 | +----------+----------+
Output: +----------+----------+ | user_id1 | user_id2 | +----------+----------+ | 4 | 5 | +----------+----------+
💡 Note: Users 1, 2, and 3 form a triangle where everyone is connected. Users 1-2 have mutual friend 3, 1-3 have mutual friend 2, and 2-3 have mutual friend 1. Users 4 and 5 are only friends with each other and have no mutual friends.
example_2.sql — Linear Chain
$ Input: Friends table: +----------+----------+ | user_id1 | user_id2 | +----------+----------+ | 1 | 2 | | 2 | 3 | | 3 | 4 | +----------+----------+
Output: +----------+----------+ | user_id1 | user_id2 | +----------+----------+ | 1 | 2 | | 2 | 3 | | 3 | 4 | +----------+----------+
💡 Note: This forms a linear chain: 1-2-3-4. Each adjacent pair has no mutual friends since there are no triangles in the friendship graph.
example_3.sql — Mixed Network
$ Input: Friends table: +----------+----------+ | user_id1 | user_id2 | +----------+----------+ | 1 | 2 | | 1 | 3 | | 2 | 4 | | 3 | 4 | | 5 | 6 | | 6 | 7 | +----------+----------+
Output: +----------+----------+ | user_id1 | user_id2 | +----------+----------+ | 1 | 2 | | 1 | 3 | | 5 | 6 | | 6 | 7 | +----------+----------+
💡 Note: Users 2 and 4 have mutual friend 3, and users 3 and 4 have mutual friend 2, so (2,4) and (3,4) are excluded. All other pairs have no mutual friends.

Visualization

Tap to expand
12345FriendsFriendsFriendsNo Mutual Friends!Triangle Network (1-2-3)• User 1 & 2: Mutual friend = 3• User 1 & 3: Mutual friend = 2• User 2 & 3: Mutual friend = 1❌ All pairs excludedIsolated Pair (4-5)• User 4's friends: {5}• User 5's friends: {4}• Mutual friends: {} (empty)✅ Include in resultsFriends With No Mutual Friends
Understanding the Visualization
1
Map the Network
Create a friendship map showing who is connected to whom
2
Analyze Each Pair
For every friendship, examine their respective social circles
3
Find Overlaps
Check if their friend groups have any people in common
4
Identify Isolated Pairs
Pairs with no overlapping friends are your answer
Key Takeaway
🎯 Key Insight: Use adjacency lists and set operations to efficiently detect mutual friends. Build the friendship network once, then leverage set intersection to quickly identify pairs without common connections.

Time & Space Complexity

Time Complexity
⏱️
O(n² × k)

Where n is number of friendships and k is average number of friends per user. Building adjacency list is O(n), checking each pair is O(n × k²) for set operations

n
2n
Quadratic Growth
Space Complexity
O(n × k)

Storage for adjacency list where each user can have up to k friends

n
2n
Linearithmic Space

Constraints

  • 1 ≤ Number of friendship pairs ≤ 104
  • 1 ≤ user_id1, user_id2 ≤ 104
  • user_id1 ≠ user_id2 (no self-friendships)
  • Each friendship pair appears only once in the table
  • Friendships are bidirectional
Asked in
Meta 45 LinkedIn 38 Google 32 Microsoft 28
43.6K Views
Medium Frequency
~25 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen