What they’re really asking
If you only remember one thing, say this in the first minute: “I’ll recommend friends of friends, ranked by how many mutual friends they share, excluding the user and people they already know. Before coding I’ll estimate the work from the average number of friends, so I know whether the simple version passes the large suite, and I’ll name the risk: a few users with huge friend lists. I’ll use the assistant in small, timed requests, check it on a five-user graph I work out by hand, and only then run the big suite with a timeout.”
The rest of this page explains each part of that answer.
Can you take an open-ended problem to a solution that passes test suites of growing size, using the assistant without losing control of your time?
A normal coding round has one hidden test set and one deadline. This round has several suites that get bigger, and an assistant: a chat tool backed by a large language model (LLM), a program trained on huge amounts of text and code that writes a plausible reply to what you type. The risk is not that the code is hard. The risk is spending twenty minutes waiting on a run that was never going to finish, or reviewing a large generated draft that solves the wrong problem. Estimating cost before running, and keeping requests small, protects the clock.
A Meta software-engineer candidate describes a friend-recommendation round with three test suites, the largest about 100k users and 1.5M friendship pairs. A second AI round had a card game where you also write tests that benchmark your algorithm over 1,000+ games (PracHub). The candidate’s advice: keep each AI request to a few minutes of work, cap every run with a timeout, and validate on small samples before the large suite.
Clarify first
- What makes a good recommendation? Most mutual friends, or a weighted score? Assume the mutual-friend count.
- Ties and limits. Top k per user, and how do ties break? Assume k = 10, ties broken by user id.
- Exclusions. Never the user, never existing friends.
- The graph. Is friendship two-way, and can pairs repeat? Assume two-way (undirected), and remove duplicates.
- Limits. Time limit for the largest suite, and the language.
Answer
Words you need first
- Adjacency set: for each user, the set of their friends. Checking “is v a friend of u?” is then one lookup.
- Degree: how many friends a user has. The average degree is d.
- Mutual friends: people who are friends with both u and v. Counting them for every friend-of-a-friend gives the ranking.
The worked example: five users you can check by hand
1 ─── 2
│ │
3 ─── 4
│
5
Friendships: 1–2, 1–3, 2–4, 3–4, 3–5. Recommend for user 1:
| Step | What happens | Running counts | What to say in the interview |
|---|---|---|---|
| 1 | User 1’s friends are {2, 3}. Skip 1, 2 and 3 from now on. | none | ”Never the user, never a current friend.” |
| 2 | Walk friend 2: its friends are {1, 4}. Skip 1; count 4. | 4 → 1 | ”Each friend-of-a-friend gets one point per shared friend.” |
| 3 | Walk friend 3: its friends are {1, 4, 5}. Skip 1; count 4 and 5. | 4 → 2, 5 → 1 | |
| 4 | Sort by count, ties by id. | [4 (2 mutual), 5 (1 mutual)] | “This is my hand answer. The code must match it before I run anything big.” |
Give this graph to the assistant as the first test. It catches the common mistakes at once: recommending user 2 or 3 (already friends), recommending user 1 to themself, or counting user 4 only once.
Build the adjacency once, then count
Store friends as a set per user, removing duplicate pairs on load. For each user u, walk each friend f and each friend of f, skip u and u’s friends, and count. Return the top k by count.
Estimate before you run the big suite
The work is roughly one set check for every friend-of-a-friend. With the average degree d, that is about d² checks per user. For the largest suite, d is about 30, so the whole run is about 90 million set checks. That is fine in compiled code, and slow but often workable in Python.
The trap is the average. A few users with huge friend lists (“celebrity” users) dominate the cost, because the cost grows with the square of each degree. One user with an assumed 10,000 friends costs about as much as the whole rest of the graph.
Say it like this: “The average case is about 90 million checks, so the simple version should pass. If it times out, it’ll be because of a few huge-degree users, and I’ll cap them.”
If the large suite does time out, cap the work per user: skip friends above a degree threshold, or sample them. Then say what that does to quality: “We’ll miss some recommendations that come through very popular users. Is that an acceptable trade for passing the time limit?”
The arithmetic behind the numbers (for a Staff-level follow-up)
work ≈ U × d² (with average degree d = 2P ÷ U) U- users
P- friendship pairs
d- average friends per user
U = 100,000, P = 1.5M: d = 3M ÷ 100k = 30, so about 100,000 × 900 = 90M set checks. That is fine in compiled code and slow but often workable in Python; a skewed graph with a few huge-degree users can be far worse, since cost follows the sum of squared degrees.
- Why squared degrees: each friend f is walked once for each of its own friends, and each walk reads all of f’s friends. So f costs deg(f) × deg(f) in total, and the whole run costs the sum of every user’s degree squared. With every degree equal to 30, that is 100,000 × 30² = 90M, the same as above.
- One celebrity (assumed): a user with 10,000 friends costs 10,000² = 100M checks alone, more than the 90M of the evenly spread graph.
Use the assistant in small, timed requests
Have it write the loader, the top-k with ties, and the tests on the five-user graph. Run each with a timeout. Only then run the 100k suite. Each request should be a few minutes of work, as the candidate advised.
Benchmark tests for the card game
When asked to benchmark over 1,000+ games, fix the random seed (the starting value for the random number generator, so the same games are dealt every run). Then time the whole batch, and report a rate (games per second) and whether the result changed between versions. Without a fixed seed, two runs play different games and can’t be compared.
Senior vs Staff
Graph problem at scale
Senior
- Derives the cost from average degree before coding, passes the suites in order, and tests on a hand-checked small graph.
Staff
- Names the degree-skew risk, decides the cap and its quality cost, and makes benchmarks reproducible with seeds.
Small-team version
The candidate’s playbook is a good team rule for work with a coding agent (an assistant that can edit files and run commands itself):
- Small requests. A few minutes of work each, so a wrong turn costs minutes, not an afternoon.
- A timeout on every run. A stuck agent loop can burn an afternoon of an engineer or a day of API spend.
- Small samples before the full dataset. A five-row test that fails in a second beats a full run that fails after twenty minutes.
For recommendation features themselves, a small company usually computes them in the database or a nightly batch job, not a separate service. Build the service only when recommendations must change within seconds of a new friendship.
Pitfalls
- Running the largest suite first, with no timeout. You learn nothing for twenty minutes.
- Recommending existing friends or the user themself. The five-user test catches both.
- Ignoring high-degree users. They dominate the cost, because cost grows with the square of the degree.
- Benchmarks without a fixed seed. Two runs play different games, so they can’t be compared.
Go deeper
- Family walkthrough: plan before code and measuring before optimizing.
- The bounded-score variant at Fireworks: r2a-025.
- Practise benchmark-driven changes on Build Your Own Kafka-lite.