Page Recommendations II - Problem

You are implementing a page recommendation system for a social media website. Your system will recommend a page to user_id if the page is liked by at least one friend of user_id and is not liked by user_id.

Given two tables:

  • Friendship: Contains friend relationships between users
  • Likes: Contains which users like which pages

Write a solution to find all the possible page recommendations for every user. Each recommendation should include:

  • user_id: The ID of the user receiving the recommendation
  • page_id: The ID of the recommended page
  • friends_likes: The number of friends who like this page

Table Schema

Friendship
Column Name Type Description
user1_id PK int First user in friendship
user2_id PK int Second user in friendship
Primary Key: (user1_id, user2_id)
Likes
Column Name Type Description
user_id PK int User who likes the page
page_id PK int Page that is liked
Primary Key: (user_id, page_id)

Input & Output

Example 1 — Basic Recommendations
Input Tables:
Friendship
user1_id user2_id
1 2
1 3
1 4
2 3
2 4
2 5
6 1
Likes
user_id page_id
1 88
2 23
3 24
4 56
5 11
6 33
2 77
3 77
6 88
Output:
user_id page_id friends_likes
1 23 1
1 24 1
1 56 1
1 33 1
1 77 2
2 24 1
2 56 1
2 11 1
2 88 2
3 88 1
3 23 1
3 56 1
4 88 1
4 77 2
5 77 1
6 23 1
6 24 1
6 56 1
6 77 2
💡 Note:

User 1 is friends with users 2, 3, 4, and 6 (bidirectional). User 1 likes page 88, so we recommend pages liked by friends but not by user 1. Page 77 is recommended with friends_likes=2 because both user 2 and user 3 like it.

Example 2 — No Recommendations
Input Tables:
Friendship
user1_id user2_id
1 2
Likes
user_id page_id
1 88
2 88
Output:
user_id page_id friends_likes
💡 Note:

Users 1 and 2 are friends and both like the same page (88). Since each user already likes the page their friend likes, there are no recommendations to make.

Constraints

  • 1 ≤ user1_id, user2_id ≤ 500
  • 1 ≤ user_id ≤ 500
  • 1 ≤ page_id ≤ 1000
  • All relationships in Friendship are unique
  • All pairs in Likes are unique

Visualization

Tap to expand
Page Recommendation System OverviewInput: Friendshipuser1_iduser2_id12Input: Likesuser_idpage_id223CTEBidirectional Friendsuser_idfriend_id1221Both directions createdJOIN + FILTERRecommendationsuser_idpage_idcount1231Pages liked by friendsbut not by userKey Steps:1. Create bidirectional friendship with CTE UNION2. JOIN friends with their liked pages3. Filter out pages already liked by target user (NOT EXISTS)4. GROUP BY to count how many friends like each page
Understanding the Visualization
1
Input Tables
Friendship relationships and user likes
2
Bidirectional Mapping
Create complete friendship graph with CTE
3
Filter & Aggregate
Find friend likes, exclude user's likes, count
Key Takeaway
🎯 Key Insight: Use bidirectional CTE to handle friendship relationships in both directions, then leverage NOT EXISTS for efficient filtering
Asked in
Meta 28 LinkedIn 22 Twitter 15
32.0K Views
Medium Frequency
~25 min Avg. Time
892 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