bugfree Icon
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course

Leetcode Problem 1949. Strong Friendship

1949. Strong Friendship

Leetcode Solutions

Finding Strong Friendships Using Common Friends

  1. Create a Common Table Expression (CTE) to union the Friendship table with itself, but with user1_id and user2_id swapped, to account for the bidirectional nature of friendships.
  2. Perform a self-join on this CTE to find pairs of users that have a common friend.
  3. Group the results by the user pairs and count the number of common friends they have.
  4. Use a HAVING clause to filter out pairs that have fewer than three common friends.
  5. Select the user pairs and their count of common friends as the final result.
  6. Ensure that the result does not contain duplicates by selecting pairs where user1_id is less than user2_id.
erDiagram
    Friendship {
        int user1_id
        int user2_id
    }

Alternative Approach Using Subquery for Common Friends

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...
bugfree Icon
OR