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 recommendationpage_id: The ID of the recommended pagefriends_likes: The number of friends who like this page
Table Schema
| Column Name | Type | Description |
|---|---|---|
user1_id
PK
|
int | First user in friendship |
user2_id
PK
|
int | Second user in friendship |
| Column Name | Type | Description |
|---|---|---|
user_id
PK
|
int | User who likes the page |
page_id
PK
|
int | Page that is liked |
Input & Output
| user1_id | user2_id |
|---|---|
| 1 | 2 |
| 1 | 3 |
| 1 | 4 |
| 2 | 3 |
| 2 | 4 |
| 2 | 5 |
| 6 | 1 |
| user_id | page_id |
|---|---|
| 1 | 88 |
| 2 | 23 |
| 3 | 24 |
| 4 | 56 |
| 5 | 11 |
| 6 | 33 |
| 2 | 77 |
| 3 | 77 |
| 6 | 88 |
| 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 |
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.
| user1_id | user2_id |
|---|---|
| 1 | 2 |
| user_id | page_id |
|---|---|
| 1 | 88 |
| 2 | 88 |
| user_id | page_id | friends_likes |
|---|
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
Friendshipare unique -
All pairs in
Likesare unique