FastPrepCount a Person’s Distinct Friends
Problems/Count a Person’s Distinct Friends

Count a Person’s Distinct Friends

Wayfair logoWayfair● EasyFULLTIMEPHONE SCREEN

Updated Nov 2026

QuestionConstraints

Problem statement

Source note: The reported query that returns a person's friend count matches at about 80%. The original says a database schema was given but does not show it. The table names, a target table with a single row, undirected counting of distinct friends, how duplicate, reversed and self rows are handled, the zero-friend output and finite limits are our assumptions. All the data shown is ours. The other system-design questions remain separate.

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

MySQLPostgreSQLPandas

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.

ColumnTypeNullableDescription
person_aIntegerNo—
person_bIntegerNo—

target_person

Exactly one authored input row identifies the person whose direct friends are counted.

ColumnTypeNullableDescription
person_idIntegerNo—

Expected result

Your query or function must return these columns.

ColumnTypeNullableDescription
friend_countIntegerNo—

Row order: must match exactly. Numeric tolerance: 0.

Constraints

  • For this exercise, assume 0 <= friendships row count <= 1000, and target_person contains 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
All Wayfair interview questionsSee Wayfair hiring insights