Count a Person’s Distinct Friends
Updated
Problem statement
Write a query that returns the number of distinct other people who are friends of the requested person.
For this exercise, assume friendships(person_a,person_b) stores undirected relationship records. The requested person appears in the exactly one row of target_person(person_id). A relationship may list that person in either endpoint column.
For this exercise, assume repeated or reversed copies of a relationship have no additional effect. Do not count self relationships. Count only direct relationships, not friends reached through other people.
Return exactly one row and one integer column named friend_count. For this exercise, assume a target without qualifying relationships returns zero. Do not modify the input tables or dataframes.
Table schema
Use the same input data with any supported language. Open the Schema tab in the editor to see the generated SQL setup or Pandas DataFrames.
friendships
Authored undirected friendship records; either endpoint can be the target. Repeated, reversed and self rows are allowed.
| Column | Type | Nullable | Description |
|---|---|---|---|
| person_a | Integer | No | — |
| person_b | Integer | No | — |
target_person
Exactly one authored input row identifies the person whose direct friends are counted.
| Column | Type | Nullable | Description |
|---|---|---|---|
| person_id | Integer | No | — |
Expected result
Your query or function must return these columns.
| Column | Type | Nullable | Description |
|---|---|---|---|
| friend_count | Integer | No | — |
Row order: must match exactly. Numeric tolerance: 0.
Constraints
- For this exercise, assume
0 <= friendships row count <= 1000, andtarget_personcontains exactly one row. - All IDs are non-null positive integers, each at most
10^9. Repeated rows, reversed pairs and self pairs are valid.
More Wayfair interview questions
- Container With Most WaterPHONE SCREEN · Seen Sep 2026
- Assign Distinct Letter Values to Minimize XORPHONE SCREEN · Seen Sep 2026
- First Three Badge Times per UserPHONE SCREEN · Seen Apr 2022
- Time of Maximum Mall OccupancyPHONE SCREEN · Seen Apr 2022
- Longest Common Contiguous User ActionsPHONE SCREEN · Seen Apr 2022
- Word Path Moving Only Right or DownPHONE SCREEN · Seen Mar 2022
- Tree Nodes with Zero or One ParentPHONE SCREEN · Seen Sep 2021
- UCB1 Multi-Armed Bandit TraceONSITE INTERVIEW · Seen Sep 2026
- Moving Cost by Volume and CategoryONSITE INTERVIEW · Seen Sep 2026
- DFS Subsequences of a String (For MLE)OA · Seen Sep 2024
Similar questions at other companies
- Second Highest SalaryPaytm · PHONE SCREENPaytm interview questions
- Employees Who Earn More Than Their ManagersNykaa · PHONE SCREENNykaa interview questions
- Employees Who Joined More Than a Week AgoNewgen Software · PHONE SCREEN
- Departments Without EmployeesDell Technologies · PHONE SCREENDell Technologies interview questions
- Second-Highest Distinct SalaryChubb · PHONE SCREENChubb interview questions
- Swap Two Gender ValuesInfosys · PHONE SCREENInfosys interview questions
- Find Rows Common to Two TablesSalesforce · PHONE SCREENSalesforce interview questions
- Count Records Present in A but Not BCapital One · PHONE SCREENCapital One interview questions
- Customer Order Count Distribution by CountryApple · PHONE SCREENApple interview questions
- Employees Earning More Than Their ManagerMygate · PHONE SCREENMygate interview questions