Reported September 2026
Mercorbreadth first search

Minimum Friend API Calls

Reported by candidates from Mercor's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live Mercor OA. Under 2s to a working solution.
Founder's read

Mercor reported this one in September 2026, and the title makes it sound like an API design puzzle. It isn't. Strip the friend-service story and you're left with shortest path in an unweighted undirected graph, source to target, counted in edges. If you've got an OA invite for Mercor and a day or two, this is a problem you can walk into calmly. The only real work is building the adjacency map from string IDs and handling the edge cases cleanly. StealthCoder sits invisibly on your screen as a safety net if your mind goes blank mid-assessment, but the pattern here is short enough to hold in your head.

The problem

A friendship service exposes one operation: querying a user returns that user's direct friends. Each query counts as one API call.
You are given the complete undirected friendship pairs for an offline practice adapter, plus source and target. Return the minimum number of friend-list API calls along any chain from source to target. Return 0 when they are the same user and -1 when no chain exists.

Function
minimumFriendApiCalls(friendships: String[][], source: String, target: String) → int

Examples
Example 1
friendships = [["a","b"],["b","c"],["c","d"]]
source = "a"
target = "d"
return = 3
The shortest chain has three friendship edges.
Example 2
friendships = [["alice","bob"]]
source = "alice"
target = "bob"
return = 1
One query reaches the direct friend.
Example 3
friendships = [["a","b"],["c","d"]]
source = "a"
target = "d"
return = -1
The users are in different connected components.

Constraints
0 <= friendships.length <= 100000
Every pair contains two distinct nonempty user IDs of at most 40 characters.
source and target are nonempty user IDs.
Duplicate friendship pairs do not change the result.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick is recognizing that each API call equals one edge traversed, so the answer is the BFS distance from source to target. Build a hash map from user ID to a set of neighbors, adding both directions for every pair. Duplicates vanish if you use sets, or you can just let the visited check absorb them. Run BFS from source with a visited set and a queue, tracking depth by level or by storing distance. Return 0 immediately if source equals target, even when source never appears in the friendships. Return -1 if the queue empties first. The common pitfalls are using DFS, which gives a path but not the shortest one, and crashing on a source missing from the map. With up to 100000 pairs, BFS is linear and fine. If you freeze on the live OA, StealthCoder can hand you this skeleton quietly.

StealthCoder is the hedge for the one pattern you didn't drill. It runs invisibly during the screen share.

If this hits your live OA

You can drill Minimum Friend API Calls cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. If you're reading this with an OA window open, you're who this was built for.

Get StealthCoder

Related leaked OAs

⏵ The honest play

You've seen the question. Make sure you actually pass Mercor's OA.

Mercor reuses patterns across OAs. If you're reading this with an OA window open, you're who this was built for. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Minimum Friend API Calls FAQ

What's the trick in Minimum Friend API Calls?+

It's a shortest path in an unweighted graph in disguise. Every friend-list query is one edge, so the minimum calls equals the BFS distance from source to target. Build an adjacency map from the pairs, run BFS, and return the depth when you reach the target.

Why BFS and not DFS?+

BFS explores level by level, so the first time you reach the target is guaranteed to be the fewest edges. DFS can find a valid chain that's longer than the best one, so it would give wrong answers unless you explore every path, which is far too slow at 100000 pairs.

What edge cases should I test before submitting?+

Test source equals target, which returns 0 even if the user isn't in the list. Test empty friendships with different users, which returns -1. Test a source or target that never appears in any pair. Test duplicate pairs and reversed duplicates, which shouldn't change anything.

How hard is this problem really?+

It's easy to medium. The algorithm is standard BFS, and most of the effort goes into building the graph from string pairs and handling the zero and negative-one cases. If you've written BFS on a grid or graph before, you can finish this quickly.

How do I prepare for this in 48 hours?+

Write BFS shortest path on an adjacency map from scratch twice, using string keys and a visited set. Practice building undirected graphs from edge lists, using a map of sets. Then run through the three examples here plus the missing-node case. That covers nearly everything this problem tests.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with Mercor.

OA at Mercor?
Invisible during screen share
Get it