Quick Overview

This question evaluates fluency with graph traversal, set operations, deduplication, and frequency-based ranking in Python, targeting manipulation of directed "follows" relationships represented as dictionaries and is categorized under Data Manipulation (SQL/Python).

Recommend two-hop follows in Python

Company: Meta

Role: Data Engineer

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Onsite

Given a directed "follows" graph as a Python dict[str, list[str]], implement recommend_two_hop(graph, user) that returns the set (or a sorted list) of accounts followed by the user’s followees that the user does not already follow, excluding the user themself. Deduplicate recommendations; if you return a list, sort by descending frequency among two-hop neighbors, then lexicographically. Example: graph = {"A": ["B","C"], "B": ["C","D"], "C": ["E"]} ⇒ recommend_two_hop(graph, "A") = {"D","E"}.

Overview: This question evaluates fluency with graph traversal, set operations, deduplication, and frequency-based ranking in Python, targeting manipulation of directed "follows" relationships represented as dictionaries and is categorized under Data Manipulation (SQL/Python).

Read the full Meta Data Engineer interview experience this question came from

You are given a directed "follows" graph stored in a SQL table. For a given user (use user_id = 'A' for this question), recommend accounts that are followed by the user's followees (i.e., two-hop neighbors), but that the user does NOT already follow. Also exclude recommending the user themself. Return one row per recommended account with: - recommended_user: the recommended account - frequency: how many of the user's direct followees follow that recommended account Deduplicate recommendations by recommended_user and compute frequency as the count of distinct direct followees that point to that recommended user. Sort results by: 1) frequency DESC 2) recommended_user ASC (lexicographically) Example intuition: if A follows B and C, and both B and C follow D, then D should appear once with frequency = 2.

Tables

follows(follower VARCHAR(50), followee VARCHAR(50))

Hints

  1. First get the user's direct followees, then join follows again to reach two hops.
  2. Exclude recommendations that are already directly followed using an anti-join (LEFT JOIN ... IS NULL) or NOT EXISTS.

Loading coding console...