Skip to main content

Page Recommendations - Solution & Explanation

MediumPremiumFree on FleetCodeDatabase4 min readAsked at: Meta
Practice this problem

Problem Statement

Table: Friendship

+---------------+---------+
| Column Name   | Type    |
+---------------+---------+
| user1_id      | int     |
| user2_id      | int     |
+---------------+---------+
(user1_id, user2_id) is the primary key (combination of columns with unique values) for this table.
Each row of this table indicates that there is a friendship relation between user1_id and user2_id.

 

Table: Likes

+-------------+---------+
| Column Name | Type    |
+-------------+---------+
| user_id     | int     |
| page_id     | int     |
+-------------+---------+
(user_id, page_id) is the primary key (combination of columns with unique values) for this table.
Each row of this table indicates that user_id likes page_id.

 

Write a solution to recommend pages to the user with user_id = 1 using the pages that your friends liked. It should not recommend pages you already liked.

Return result table in any order without duplicates.

The result format is in the following example.

 

Example 1:

Input: 
Friendship table:
+----------+----------+
| user1_id | user2_id |
+----------+----------+
| 1        | 2        |
| 1        | 3        |
| 1        | 4        |
| 2        | 3        |
| 2        | 4        |
| 2        | 5        |
| 6        | 1        |
+----------+----------+
Likes table:
+---------+---------+
| user_id | page_id |
+---------+---------+
| 1       | 88      |
| 2       | 23      |
| 3       | 24      |
| 4       | 56      |
| 5       | 11      |
| 6       | 33      |
| 2       | 77      |
| 3       | 77      |
| 6       | 88      |
+---------+---------+
Output: 
+------------------+
| recommended_page |
+------------------+
| 23               |
| 24               |
| 56               |
| 33               |
| 77               |
+------------------+
Explanation: 
User one is friend with users 2, 3, 4 and 6.
Suggested pages are 23 from user 2, 24 from user 3, 56 from user 3 and 33 from user 6.
Page 77 is suggested from both user 2 and user 3.
Page 88 is not suggested because user 1 already likes it.

Approach Overview

Problem Overview: You need to recommend new pages to a user based on what their friends like. A page should appear in the result if a friend liked it but the user has not already liked it.

Approach 1: Union + Equi-Join + Subquery (O(F + L) time, O(F) space)

The core idea is to first identify all friends of the target user. Since the friendship table stores relationships in two columns (user1_id, user2_id), a UNION query extracts both directions so you get a clean list of friend IDs. Next, perform an equi-join between this friend list and the Likes table to find pages those friends liked. Finally, exclude any pages already liked by the user using a subquery filter such as NOT IN. Add DISTINCT to remove duplicates when multiple friends like the same page. This approach is easy to reason about and maps directly to relational operations using database queries, joins, and subqueries.

Approach 2: Default Approach (Self-Filtering Join) (O(F + L) time, O(1) extra space)

This approach performs the recommendation in a single query pipeline. Join the Friendship table with the Likes table to map each friend to the pages they liked. A conditional filter extracts friends connected to the target user from either side of the friendship relation. Then apply a NOT IN or LEFT JOIN ... IS NULL filter against the user's existing likes to remove pages the user already follows. Use DISTINCT on page_id to avoid duplicate recommendations when multiple friends like the same page. This pattern is common in SQL interview problems where relationship tables and preference tables must be combined efficiently.

Recommended for interviews: The union-based approach is the clearest way to normalize the friendship relationship before joining with likes. Interviewers typically expect you to correctly handle the bidirectional friendship table and filter out existing likes. Showing both the union extraction and the exclusion subquery demonstrates strong understanding of relational joins and filtering logic.

Approach 1: Union + Equi-Join + Subquery

First, we query all users who are friends with user_id = 1 and record them in the T table. Then, we query all pages that users in the T table like, and finally exclude the pages that user_id = 1 likes.

Code

MySQL

Try this approach in the editor →

Approach 2: Default Approach

Code

MySQL

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
Union + Equi-Join + Subquery—
Default Approach—

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Union + Equi-Join + SubqueryO(F + L)O(F)When friendship relationships are stored in two columns and must be normalized before joining
Default Join + Filter ApproachO(F + L)O(1)When writing a compact SQL query that directly joins friendships with likes

Video Solution

FACEBOOK/META LeetCode Medium 1264 “Page Recommendations" Interview SQL Question Explanation | EDS • Everyday Data Science • 1,835 views views

Watch 3 more video solutions →

Frequently Asked Questions

Is Page Recommendations easy or hard?
Page Recommendations is considered a medium-level SQL problem. The difficulty comes from correctly handling the bidirectional friendship table and filtering out pages the user already liked.
Page Recommendations Python/Java solution
This problem is typically solved using SQL rather than Python or Java because the dataset exists in relational tables. However, the same logic can be implemented using hash sets: collect friends, gather pages they like, and remove pages already liked by the user.
How to solve Page Recommendations in O(F + L)?
First create a friend list using UNION on the friendship table to collect all friend IDs of the user. Join that result with the Likes table to retrieve pages liked by those friends. Finally exclude pages already liked by the user using a NOT IN or LEFT JOIN filter.
What is the best approach for Page Recommendations?
The most reliable solution uses UNION to extract all friends of the target user, then joins this list with the Likes table to find pages liked by friends. A NOT IN subquery filters out pages already liked by the user. This method keeps the logic clear and handles bidirectional friendship relationships correctly.
Is Page Recommendations asked at Google/Amazon/Meta?
SQL recommendation-style queries appear frequently in interviews at companies like Meta, Amazon, and Google. Problems involving relationship tables, joins, and filtering logic are common in data engineering and backend roles.
What data structure is used in Page Recommendations?
The problem relies on relational database tables and SQL operations rather than traditional in-memory data structures. Key operations include joins, unions, filtering subqueries, and deduplication using DISTINCT.
What is the time complexity of Page Recommendations?
The query typically runs in O(F + L) time where F is the number of friendship rows scanned and L is the number of likes processed during the join. Database indexing on user_id and page_id can significantly reduce actual execution time.

Ready to solve this problem?

Practice Page Recommendations with our built-in code editor and test cases.

Practice on FleetCode