← Questions

Recommend accounts your followees follow

0
MetaCodingLast asked Aug 2025

A coding interview question asked in Meta's AI-enabled coding round, last asked Aug 2025. It tests python, graphs and hash maps.

A follow graph is given as a dictionary from each user to the list of people they follow. For a user U, recommend everyone followed by the people U follows, leaving out U and anyone U already follows.

Interface

Unless you agree on a different interface with the interviewer, assume:

  • recommend(graph: dict[str, list[str]], user: str) -> list[str] — the recommendations without duplicates, sorted alphabetically so the output is deterministic. A user who is not a key follows nobody.

Example

graph = {"A": ["B", "C"], "B": ["C", "D"], "C": ["E"]}

recommend(graph, "A")   ->  ["D", "E"]     # C is excluded: A already follows C
recommend(graph, "B")   ->  ["E"]          # D follows nobody
recommend(graph, "E")   ->  []             # E follows nobody

Constraints

  • Up to 10^5 users and 10^6 follow edges.
  • Self-follows and mutual follows may appear.

Clarifying questions

  • Is the graph directed (follows) or undirected (friendships)?
  • Should the result be ranked, and by what?
  • Can a user appear only as a followee and never as a key?
  • Is the user themselves ever in the candidates? (Yes, through a mutual follow; exclude them.)

What a strong answer covers

  • A set of U's direct follows for O(1) checks, a loop over each followee's follows, and a result set that removes U, direct follows and duplicates.
  • O(sum of the followees' out-degrees) time and O(candidates) space.
  • Using graph.get(v, []) for users with no entry, and deduplicating repeated edges.
  • Tests: an unknown user, mutual follows, self-follows, a followee with no entry.

Follow-ups

  • Rank the recommendations by how many of U's followees follow each one. With {"A": ["B", "C", "D"], "B": ["E", "A"], "C": ["E", "F"], "D": ["E", "B"]}, user A gets E (3 followees) before F (1).
  • Return only the top k for users who follow thousands of people.
  • Compute recommendations for every user at once on a graph too large for one machine.

Where it's asked

  • Meta · AI-enabled coding · Last asked Aug 202560 min · Several progressive tasks on a starter codebase in CoderPad with an AI assistant panel; usually two rounds · The first task usually without AI, later tasks with it

What it tests

python · graphs · hash maps · data engineering

Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding