FastPrepCount a Person’s Distinct Friends

Count a Person’s Distinct Friends

Wayfair logoWayfair● EasyFULLTIMEPHONE SCREEN

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

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 problems

See Wayfair hiring insights