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 NameType
user1_idint
user2_idint

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_id format

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
Strong Friendship Network AnalysisABStrong FriendshipCDEFGMutual Friends of A: C, D, EMutual Friends of B: F, G, ECommon: C, D, E, F, G = 5 mutual friends โ‰ฅ 3 โœ“Strong Friendship Confirmed!A and B share 5 mutual friends (C, D, E, F, G), exceeding the threshold of 3
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

n
2n
โšก Linearithmic
Space Complexity
O(n)

Space for intermediate join results and grouping

n
2n
โšก 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
Asked in
Meta 45 Google 38 LinkedIn 52 Twitter 29
34.2K Views
High Frequency
~25 min Avg. Time
1.4K 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