FastPrepMinimum Friend API Calls
Problems/Minimum Friend API Calls

Minimum Friend API Calls

Mercor logoMercor● MediumFULLTIMEONSITE INTERVIEW
QuestionFunctionExamplesConstraints

Problem statement

Source note: The interview report establishes the exercise identity and central capability. This practice contract supplies only the smallest portable interface, deterministic boundary rules, and output representation needed for consistent execution.

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.

More Mercor problems

  • Expected Fake-Name CollisionsPHONE SCREEN · Seen Oct 2026
  • Length of a Document Reached Through a Random TokenPHONE SCREEN · Seen Oct 2026
  • Precision of Stacked PII Detector FlagsPHONE SCREEN · Seen Oct 2026
All Mercor interview questionsSee Mercor hiring insights
Saved
class Solution {
    public int minimumFriendApiCalls(String[][] friendships, String source, String target) {
      // write your code here
    }
}
Checking account…