- Denisse's Newsletter
- Posts
- 🔥A wild LeetCode problem appears🔥
🔥A wild LeetCode problem appears🔥
🪄The Magic (and Mystery) of Facebook's Friend Recommender

TLDR
Welcome!
DFS & BFS in the wild
Recommendations I live by!
Resources to check out
Let me invoke you into my cult
JOBS & SCHOLARSHIPS
Hi and welcome back, brave human!
This is your friendly reminder that a skill well-learned will never be forgotten!!!!
Once you solve a challenging problem, something in your brain has permanently changed.
The neural pathways formed during deep learning remain with you even when not actively used. (or at least brainwash yourself into thinking this!)
Today, we are going to see a LeetCode problem in the wild appear.
Learn AI in 5 minutes a day
This is the easiest way for a busy person wanting to learn AI in as little time as possible:
Sign up for The Rundown AI newsletter
They send you 5-minute email updates on the latest AI news and how to use it
You learn how to become 2x more productive by leveraging AI
Facebook Friends Recommendation System
Facebook’s social network is represented as a directed graph where:
Nodes (vertices) represent users.
Edges (links) represent directed relationships (e.g., "User A follows User B").
The challenge? Predicting missing edges—figuring out who should be added to your network!!! ( I know its your CS 101 crush ;))
The Leetcode Connection: DFS & BFS
Facebook uses sophisticated algorithms like PageRank, Adar Index, and Katz Centrality.
But at its heart, this is a graph traversal problem.
If you’ve solved problems like Connected Components or Flood Fill, you’ve already built the core of a Friend Recommendation System.
1. Connected Components → Finding Friend Groups
When you run DFS (Depth-First Search) or BFS (Breadth-First Search), you’re essentially identifying clusters of users who are directly or indirectly connected. In Facebook’s case, weakly connected components indicate groups of users who might belong to the same community (e.g., same college, same workplace).
Example:
You have some classmates.
Some of them might know each other.
Others might know each other too.

DFS
2. Shortest Path → Friend Recommendation Score
Using BFS, we can calculate the shortest path between two users.
If the shortest path between two users is 1 (i.e., they are already friends), no recommendation is needed.
If the shortest path is 2 (i.e., your friend’s friend), the recommendation score is high.

BFS
Why This Matters
If you've solved problems on graph traversal, cycle detection, or shortest path, you already understand the backbone of how Facebook (and many other social networks) build their friend recommendation engines.
Next time you see a People You May Know suggestion, think about how DFS, BFS, and shortest paths are working behind the scenes to bring you closer to your next connection!!!!
Recommendations
Level up your inbox by subscribing to what I am subscribed to!
Let me invoke you into my cult
Media worth seeing
Mira Murati just created the first AI company run by adults apply here
Don’t blame me if you start seeing hearts everywhere, how to rewire your brain to find opportunities
Jobs and Scholarships
Cheers to you hacking your week!
Denisse
Reply