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 33 1
1 56 1
1 77 2
2 11 1
2 24 1
2 56 1
2 88 1
3 23 1
3 88 1
4 23 1
4 77 1
4 88 1
5 23 1
5 77 1
💡 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 Recommendations II INPUT Friendship Table user1_id user2_id 1 2 1 3 2 3 Likes Table user_id page_id 1 88 2 23 3 24 2 77 Social Graph U1 U2 U3 ALGORITHM STEPS 1 Build Friendship Graph Union user1, user2 as bidirectional friends 2 Get Friends' Liked Pages JOIN Friendship with Likes to find pages friends like 3 Exclude User's Own Likes Filter pages already liked by the user (NOT IN) 4 Count Friend Likes GROUP BY user, page COUNT distinct friends SELECT user_id, page_id, COUNT(friend_id) AS cnt FROM all_friends f JOIN Likes ON friend=user WHERE page NOT IN user_likes GROUP BY user_id, page_id FINAL RESULT Page Recommendations user_id page_id friends 1 23 1 1 24 1 1 77 1 2 24 1 2 88 1 3 88 1 Example: User 1 U1 Friends: U2, U3 U2 likes: 23, 77 U3 likes: 24 U1 already likes: 88 Recommend: 23, 24, 77 OK - All recommendations found! Key Insight: Handle bidirectional friendships by creating UNION of (user1, user2) and (user2, user1) pairs. Use LEFT JOIN with NOT EXISTS or NOT IN to filter out pages the user already likes. GROUP BY user_id, page_id and COUNT to get the number of friends who like each page. TutorialsPoint - Page Recommendations II | Optimal Solution
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