Strong Friendship - Problem
Strong Friendship
In a social network, friendships are stored in a database table where each row represents a mutual friendship between two users. The challenge is to identify strong friendships - pairs of friends who have a robust social connection.
A friendship between users x and y is considered strong if they have at least three mutual friends. This indicates a deeper social bond beyond just being directly connected.
Table: Friendship
| Column Name | Type |
|---|---|
| user1_id | int |
| user2_id | int |
Key Points:
- Each row represents a bidirectional friendship
user1_id < user2_id(normalized format)- Find all strong friendships with at least 3 mutual friends
- Return results without duplicates in
user1_id < user2_idformat
Input & Output
example_1.sql โ Basic Strong Friendship
$
Input:
Friendship table:
+----------+----------+
| user1_id | user2_id |
+----------+----------+
| 1 | 2 |
| 1 | 3 |
| 1 | 4 |
| 2 | 3 |
| 2 | 4 |
| 3 | 4 |
+----------+----------+
โบ
Output:
+----------+----------+
| user1_id | user2_id |
+----------+----------+
| 1 | 2 |
| 1 | 3 |
| 1 | 4 |
| 2 | 3 |
| 2 | 4 |
| 3 | 4 |
+----------+----------+
๐ก Note:
All pairs are strong friendships because in this complete graph of 4 users, every pair shares exactly 2 mutual friends, but we need at least 3. Actually, let me recalculate: (1,2) share friends 3,4; (1,3) share friends 2,4; (1,4) share friends 2,3; (2,3) share friends 1,4; (2,4) share friends 1,3; (3,4) share friends 1,2. Each pair has exactly 2 mutual friends, so none qualify as strong friendships.
example_2.sql โ Larger Network
$
Input:
Friendship = [[1,2],[1,3],[1,4],[1,5],[2,3],[2,4],[2,5],[3,4],[3,5]]
โบ
Output:
+----------+----------+
| user1_id | user2_id |
+----------+----------+
| 1 | 2 |
| 1 | 3 |
| 2 | 3 |
+----------+----------+
๐ก Note:
Users 1 and 2 share mutual friends 3, 4, 5 (count=3). Users 1 and 3 share mutual friends 2, 4, 5 (count=3). Users 2 and 3 share mutual friends 1, 4, 5 (count=3). All these pairs have exactly 3 mutual friends, meeting the threshold.
example_3.sql โ No Strong Friendships
$
Input:
Friendship table:
+----------+----------+
| user1_id | user2_id |
+----------+----------+
| 1 | 2 |
| 2 | 3 |
| 3 | 4 |
| 4 | 1 |
+----------+----------+
โบ
Output:
Empty result set
๐ก Note:
This forms a cycle where each user has exactly 2 friends, so no pair of friends can have 3 mutual friends. Maximum possible mutual friends for any pair is 2, which is below the threshold of 3.
Visualization
Tap to expand
Understanding the Visualization
1
Original Friendships
Start with direct friendship connections in the network
2
Find Mutual Friends
For each friendship pair, identify their common connections
3
Count Connections
Count mutual friends for each pair
4
Filter Strong Bonds
Keep only pairs with 3+ mutual friends
Key Takeaway
๐ฏ Key Insight: Strong friendships are identified by counting mutual connections efficiently using SQL self-joins, filtering for pairs with 3+ shared friends.
Time & Space Complexity
Time Complexity
O(n log n)
Optimized joins with database indexes, logarithmic lookup for each friendship
โก Linearithmic
Space Complexity
O(n)
Space for intermediate join results and grouping
โก Linearithmic Space
Constraints
- 1 โค user1_id, user2_id โค 104
- user1_id < user2_id (normalized friendship format)
- 1 โค number of friendships โค 105
- Strong friendship requires โฅ 3 mutual friends
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code