logo

Back to questions

Event Friends Recommendation

Hard

Facebook wants to create a new algorithm of friend recommendations based on people who are showing interest in attending 2 or more private events.

The result table should not contain duplicates (i.e., user y should not be recommended to user x multiple times). Return the results in user_a, user_b order.

Notes:

  • A user interested in attending would have either 'going' or 'maybe' as their attendance status.
  • Friend recommendations are unidirectional, meaning if user x and user y should be recommended to each other, the result table should have both user x recommended to user y and user y recommended to user x.

Table:

Column NameType
user_a_idinteger
user_b_idinteger
statusenum ('friends', 'not_friends')

Each row of this table indicates the status of the friendship between user_a_id and user_b_id.

Example Input:

user_a_iduser_b_idstatus
111333not_friends
222333not_friends
333222not_friends
222111friends
111222friends
333111not_friends

Table:

Column NameType
user_idinteger
event_idinteger
event_typeenum ('public', 'private')
attendance_statusenum ('going', 'maybe', 'not_going')
event_datedate

Example Input:

user_idevent_idevent_typeattendance_statusevent_date
111567publicgoing07/12/2022
222789privategoing07/15/2022
333789privatemaybe07/15/2022
111234privatenot_going07/18/2022
222234privategoing07/18/2022
333234privategoing07/18/2022

Example Output:

user_a_iduser_b_id
222333
333222

User 222 and 333 who are not friends have showed the same interest in attending 2 or more private events.

PostgreSQL 14

Refer friends to get bonus content & cool prizes.