Had seen this floating around before and never actually sat down to work through it.
Start by clarifying requirements and scale, then design a graph-based data model with efficient follow/unfollow operations. For friend-of-friend recommendations, use a breadth-first search up to depth 2, aggregating mutual connections and ranking by count, while addressing scalability with sharding and caching.
Pro tip: Emphasize trade-offs between different approaches (e.g., BFS vs. precomputed recommendations) and discuss how to handle large-scale graphs with partitioning and asynchronous updates. Mention that recommendations can be computed offline and served via a fast lookup service.
Ask about expected number of users, average connections, read/write ratio, latency requirements, and whether recommendations need to be real-time or can be batch-computed.
Propose a graph representation: users as nodes, follows as directed edges. Use a distributed database like Cassandra or a graph DB (e.g., Neo4j) for storage, with adjacency lists for efficient traversal.
Describe how to add/remove edges: update the follower's following list and the followee's followers list. Discuss consistency, idempotency, and handling high write throughput.
Explain BFS from the user up to depth 2 to find candidates, then rank by number of mutual friends. For scalability, consider precomputing recommendations offline using MapReduce or graph processing frameworks.
Discuss sharding by user ID, caching frequent recommendations, using approximate algorithms for large graphs, and ensuring low-latency reads via denormalization.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.