Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Home
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets

Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Back to Problems

602. Friend Requests II: Who Has the Most Friends

Medium59.2% Acceptance
Database
Asked by:
A
Amazon
F
Facebook
ProblemHints (1)Solutions (3)VideosCompanies (2)Notes

Problem Statement

Table: RequestAccepted

+----------------+---------+
| Column Name    | Type    |
+----------------+---------+
| requester_id   | int     |
| accepter_id    | int     |
| accept_date    | date    |
+----------------+---------+
(requester_id, accepter_id) is the primary key (combination of columns with unique values) for this table.
This table contains the ID of the user who sent the request, the ID of the user who received the request, and the date when the request was accepted.

Write a solution to find the people who have the most friends and the most friends number.

The test cases are generated so that only one person has the most friends.

The result format is in the following example.

Example 1:

Input: 
RequestAccepted table:
+--------------+-------------+-------------+
| requester_id | accepter_id | accept_date |
+--------------+-------------+-------------+
| 1            | 2           | 2016/06/03  |
| 1            | 3           | 2016/06/08  |
| 2            | 3           | 2016/06/08  |
| 3            | 4           | 2016/06/09  |
+--------------+-------------+-------------+
Output: 
+----+-----+
| id | num |
+----+-----+
| 3  | 3   |
+----+-----+
Explanation: 
The person with id 3 is a friend of people 1, 2, and 4, so he has three friends in total, which is the most number than any others.

Follow up: In the real world, multiple people could have the same most number of friends. Could you find all these people in this case?

Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions

Approach

In #602 Friend Requests II: Who Has the Most Friends, the goal is to determine which user has the highest number of friends based on accepted friend requests. Each row in the table represents a confirmed friendship between a requester_id and an accepter_id. Since friendships are mutual, both users should be counted toward each other's total.

A practical strategy is to normalize the relationships by treating both columns as individual user entries. This can be achieved by combining the requester_id and accepter_id columns using a UNION ALL. After transforming the data into a single list of user IDs, you can apply GROUP BY to count how many times each user appears, which corresponds to their number of friends.

Finally, sort the counts in descending order and return the user with the highest value. This approach efficiently aggregates friendship counts using SQL grouping and sorting operations, making it scalable even for larger datasets.

Complexity

ApproachTime ComplexitySpace Complexity
UNION ALL with GROUP BY aggregationO(n)O(n)

Video Solution Available

Everyday Data Science

View all video solutions

Problem Hints

Use these hints if you're stuck. Try solving on your own first.

1
Hint 1

Being friends is bidirectional. If you accept someone's adding friend request, both you and the other person will have one more friend.

Ready to see the solutions?View Solutions

Solutions (3)

SQL Aggregation and Joined Count

The main idea here is to see every occurrence of an ID as a friendship connection, whether it appears as a requester or an accepter. We'll thus aggregate the ID counts to find out who has the maximum number of occurrences, meaning the maximum number of friends.

First, we can treat requesters and accepters separately and count how many times a person appears as a requester or an accepter, then sum these counts to get the total number of friends for each ID. Finally, we can select the ID with the maximum sum of appearances.

Time Complexity: O(n log n) due to sorting operation. Space Complexity: O(n) for storing the consolidated list of all IDs.

1SELECT id, num FROM ( SELECT id, COUNT(id) as num FROM ( SELECT requester_id as id FROM RequestAccepted UNION ALL SELECT accepter_id as id FROM RequestAccepted ) as friends GROUP BY id ) as counts ORDER BY num DESC LIMIT 1;

Explanation

We first create a subquery to combine all requesters and accepters into a single list using UNION ALL. We then count the occurrences of each individual in this list, which represents the number of friendships they have. Finally, we sort these counts in descending order and select the top result.

Use of Dictionary (HashMap) to Count Friends

This approach can be employed in programming languages such as Python, Java, and C# where dictionaries (or hashmaps) are effective for counting occurrences.

For each row in the data, increment the friendship count for both the requester and the accepter in a dictionary (or hashmap). After processing, iterate through the dictionary to find the ID with the maximum count.

Time Complexity: O(n) since we pass through the data once and dictionary operations are generally O(1). Space Complexity: O(n) for storing the friend counts.

PythonC++
1def find_most_friends(requests):
2

Video Solutions

Watch expert explanations and walkthroughs

FACEBOOK/META LeetCode Medium “Friend Requests II" 602 Interview SQL Question Explanation | EDS

Everyday Data Science
7:094,653 views

Asked By Companies

2 companies
A
Amazon
F
Facebook

Prepare for Interviews

Practice problems asked by these companies to ace your technical interviews.

Explore More Problems

Notes

Personal Notes

Jot down your thoughts, approach, and key learnings

0 characters

Similar Problems

Combine Two TablesEasy
Second Highest SalaryMedium
Nth Highest SalaryMedium
Rank ScoresMedium
More similar problems

Related Topics

Database

Problem Stats

Acceptance Rate59.2%
DifficultyMedium
Companies2

Practice on LeetCode

Solve with full IDE support and test cases

Solve Now

Frequently Asked Questions

Why do we use UNION ALL in Friend Requests II: Who Has the Most Friends?

UNION ALL is used to merge requester_id and accepter_id into one list without removing duplicates. This ensures every accepted friendship contributes correctly to each user's total friend count.

Is Friend Requests II: Who Has the Most Friends asked in FAANG interviews?

Yes, similar SQL aggregation and relationship-counting problems are common in FAANG-style interviews. They test your ability to manipulate relational data, normalize relationships, and use grouping and sorting effectively.

What is the optimal approach for Friend Requests II: Who Has the Most Friends?

The optimal approach is to combine requester_id and accepter_id into a single column using UNION ALL, then group by the user ID to count friendships. Sorting the counts in descending order helps identify the user with the maximum number of friends.

What data structure or SQL concept is best for Friend Requests II: Who Has the Most Friends?

SQL aggregation using GROUP BY is the key concept for this problem. By transforming both columns into a unified list of user IDs, you can efficiently count friendships and determine which user appears most frequently.

Previous Problem

Human Traffic of Stadium

Next Problem

Consecutive Available Seats

from
collections
import
defaultdict
3
friend_count
=
defaultdict
(
int
)
4
for
request
in
requests
:
5
requester
,
accepter
=
request
6
friend_count
[
requester
]
+=
1
7
friend_count
[
accepter
]
+=
1
8
max_friends
=
max
(
friend_count
.
values
(
)
)
9
for
person
,
count
in
friend_count
.
items
(
)
:
10
if
count
==
max_friends
:
11
return
person
,
count
12
requests
=
[
(
1
,
2
)
,
(
1
,
3
)
,
(
2
,
3
)
,
(
3
,
4
)
]
13
print
(
find_most_friends
(
requests
)
)
# Output: (3, 3)

Explanation

Using the Python defaultdict, we initialize counts to zero and update them by iterating over each friendship in the list. After updating, we calculate the maximum count and figure out which person has this maximum count.