Implement friend recommendations (people you may know) for a social graph

Quick Overview

A coding exercise to build a friend recommendation feature for a social network, suggesting people a user may know from their friends' connections. It tests clarifying the ranking rule, counting shared friends efficiently, top-k selection, edge cases, and reviewing code written with an AI assistant.

Implement friend recommendations (people you may know) for a social graph

Company: Meta

Role: Software Engineer

Category: Software Engineering Fundamentals

Difficulty: medium

Interview Round: Technical Screen

Implement friend recommendations ("people you may know") for a social network: given the users and their friendships, return the people to suggest to one user as new friends. The task was set in a coding round where an AI coding assistant was available in the editor, and the code was expected to run. The reported prompt names only the feature, so the input format, the ranking rule and the number of suggestions are open. Unless the interviewer says otherwise, confirm and use this common baseline: friendships are mutual; a candidate is anyone who is not the user, is not already the user's friend, and has at least one friend in common with the user; candidates are ranked by how many friends they share with the user, and the top `k` are returned. A self-contained version of the task: ```python def recommend_friends(friendships: list[tuple[int, int]], user: int, k: int) -> list[int]: ``` For example, with friendships `(1, 2), (1, 3), (2, 4), (3, 4), (3, 5), (4, 6)` and `user = 1`, user 4 shares two friends with user 1 (users 2 and 3) and user 5 shares one (user 3), so the baseline with `k = 2` returns `[4, 5]`. User 6 shares no friend with user 1 and is not suggested. ```hint Two hops away Work out exactly which people can ever be suggested, and which of them must be filtered out before any scoring. ``` ```hint One scan for every score Think about how much of the graph you need to touch, and whether a single scan can produce every candidate's score at once instead of comparing the user with each person separately. ``` ```hint Keep the assistant on task Decide which pieces you would hand to the assistant, and how you would check that its edits stay inside the task. ``` ### Clarifying Questions - Are friendships mutual, or one-way follows? Can the input contain duplicate pairs, or a user paired with themselves? - Is the number of shared friends the only ranking signal, and how are ties broken (for example by smaller user id)? - Should blocked users, pending requests or previously dismissed suggestions be excluded? - What should a user with no friends, or a user who is not in the data, get back: an empty list or some fallback? - How large is the graph, and how long can one user's friend list get? Is the function called once, or repeatedly while friendships change? - Is the code written from scratch, or added to an existing codebase whose classes the assistant can read and edit? ### What a Strong Answer Covers - The candidate set and ranking rule agreed before coding, including exclusions and tie-breaking - A graph representation and a counting pass over friends of friends that excludes the user and existing friends correctly - Top-k selection and its cost compared with sorting every candidate - Running time in terms of the user's friends and their friend counts, and behavior around very high-degree users - Edge cases: no friends, unknown user, duplicate or self pairs, `k` larger than the number of candidates - Working, tested code, with every change from the AI assistant reviewed and kept to the task ### Follow-up Questions - Ranking by the raw number of shared friends treats a shared friend with thousands of friends the same as a close one. How would you weight shared friends instead? - Suggestions are requested far more often than friendships change. What would you precompute, and what must be updated when two users become friends? - One of the user's friends has millions of friends. How do you keep a single request fast? - How would you test the function, and what would you ask the assistant to generate versus write yourself?

Overview: A coding exercise to build a friend recommendation feature for a social network, suggesting people a user may know from their friends' connections. It tests clarifying the ranking rule, counting shared friends efficiently, top-k selection, edge cases, and reviewing code written with an AI assistant.

Meta logo
Meta
Nov 13, 2026
mediumSoftware EngineerTechnical ScreenSoftware Engineering Fundamentals
2
0

Implement friend recommendations ("people you may know") for a social network: given the users and their friendships, return the people to suggest to one user as new friends. The task was set in a coding round where an AI coding assistant was available in the editor, and the code was expected to run.

The reported prompt names only the feature, so the input format, the ranking rule and the number of suggestions are open. Unless the interviewer says otherwise, confirm and use this common baseline: friendships are mutual; a candidate is anyone who is not the user, is not already the user's friend, and has at least one friend in common with the user; candidates are ranked by how many friends they share with the user, and the top k are returned.

A self-contained version of the task:

def recommend_friends(friendships: list[tuple[int, int]], user: int, k: int) -> list[int]:

For example, with friendships (1, 2), (1, 3), (2, 4), (3, 4), (3, 5), (4, 6) and user = 1, user 4 shares two friends with user 1 (users 2 and 3) and user 5 shares one (user 3), so the baseline with k = 2 returns [4, 5]. User 6 shares no friend with user 1 and is not suggested.

Clarifying Questions Guidance

  • Are friendships mutual, or one-way follows? Can the input contain duplicate pairs, or a user paired with themselves?
  • Is the number of shared friends the only ranking signal, and how are ties broken (for example by smaller user id)?
  • Should blocked users, pending requests or previously dismissed suggestions be excluded?
  • What should a user with no friends, or a user who is not in the data, get back: an empty list or some fallback?
  • How large is the graph, and how long can one user's friend list get? Is the function called once, or repeatedly while friendships change?
  • Is the code written from scratch, or added to an existing codebase whose classes the assistant can read and edit?

What a Strong Answer Covers Guidance

  • The candidate set and ranking rule agreed before coding, including exclusions and tie-breaking
  • A graph representation and a counting pass over friends of friends that excludes the user and existing friends correctly
  • Top-k selection and its cost compared with sorting every candidate
  • Running time in terms of the user's friends and their friend counts, and behavior around very high-degree users
  • Edge cases: no friends, unknown user, duplicate or self pairs, k larger than the number of candidates
  • Working, tested code, with every change from the AI assistant reviewed and kept to the task

Follow-up Questions Guidance

  • Ranking by the raw number of shared friends treats a shared friend with thousands of friends the same as a close one. How would you weight shared friends instead?
  • Suggestions are requested far more often than friendships change. What would you precompute, and what must be updated when two users become friends?
  • One of the user's friends has millions of friends. How do you keep a single request fast?
  • How would you test the function, and what would you ask the assistant to generate versus write yourself?