FastPrepMaximum Friendship Hops
Problems/Maximum Friendship Hops

Maximum Friendship Hops

Google logoGoogle● MediumFULLTIMEONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: The report asks for the maximum friendship hops and mentions BFS. It leaves out the starting points and the rules for the snapshot and graph. We assume the answer is the largest shortest distance over all pairs in a connected, undirected snapshot. Our version is about 65% the same as the reported question.

Find the largest friendship hop count. For this exercise, assume the supplied graph is one connected, undirected friendship snapshot with n people numbered from 0 to n - 1.

Each row of friendships is [u, v] and represents a link usable in either direction. A hop traverses one link. For each pair of people, use the fewest possible hops between them; return the largest of these shortest distances over all pairs.

For this exercise, assume a person has distance 0 to themselves. Duplicate links and self-links have no effect on the answer. Cycles are allowed: do not return the length of a longest simple route, and do not assume the graph is a tree.

The input already describes the selected snapshot. There are no timestamps or updates to process, and no starting person is supplied. The one-person graph returns 0.

Function

maxFriendshipHops(n: int, friendships: int[][]) → int

Examples

Example 1

n = 5friendships = [[0,1],[1,2],[2,3],[3,4]]return = 4

The endpoints need four links; every other pair needs at most four.

Example 2

n = 4friendships = [[0,1],[1,2],[2,3],[3,0]]return = 2

Opposite vertices need two links. Taking a longer route around the cycle does not increase their shortest distance.

Example 3

n = 3friendships = [[0,1],[0,2],[1,2]]return = 1

Every distinct pair has a direct friendship.

Constraints

  • For this exercise, assume 1 <= n <= 300.
  • 0 <= friendships.length <= 2000 and every row contains exactly two labels in [0, n - 1].
  • The graph is connected and undirected. Duplicate links, self-links and cycles are allowed.

More Google Onsite Interview questions

  • Route Pattern MatcherPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Evaluate a Nested Math ExpressionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Distinct Subset SumsONSITE INTERVIEW · Seen Nov 2026
  • Playlist With No Same Artist in a RowONSITE INTERVIEW · Seen Nov 2026
  • Delete Nodes and Return Forest RootsONSITE INTERVIEW · Seen Nov 2026
  • Dice-Walk Winning ProbabilityPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • First and Last Target Position in a Mountain ArrayONSITE INTERVIEW · Seen Nov 2026
  • Lowest Common Ancestor in an N-ary TreePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • 0/1 Knapsack with Weight and Volume LimitsONSITE INTERVIEW · Seen Nov 2026
  • Batched Subsequence Word MatchesONSITE INTERVIEW · Seen Nov 2026

Similar questions at other companies

  • Course ScheduleAmazon · PHONE SCREEN · ONSITE INTERVIEW
  • Course ScheduleOracle · ONSITE INTERVIEWOracle interview questions
  • Course SchedulePlaud · ONSITE INTERVIEW
  • Explore an Unknown Grid and Find a PathLinkedIn · ONSITE INTERVIEWLinkedIn interview questions
  • Nearest Charging Station on a GridSpotify · ONSITE INTERVIEW
  • Hierarchical Permission CheckeBay · ONSITE INTERVIEWeBay interview questions
  • Evaluate DivisionPhonePe · ONSITE INTERVIEWPhonePe interview questions
  • Count Friend CirclesOpenAI · PHONE SCREEN · ONSITE INTERVIEWOpenAI interview questions
  • Finite Web CrawlerMongoDB · PHONE SCREEN · ONSITE INTERVIEWMongoDB interview questions
  • Amount of Time to Infect a TreeMicrosoft · ONSITE INTERVIEWMicrosoft interview questions

Google interview experiences

  • Google interviews · November 2026
  • Google Software Engineer Onsite Questions: 10 Experiences
  • Google OA: What It Tests (2026)
  • Google L4 SWE Phone Screen: 1 Experience
  • Google ML Engineering Manager Onsite: 1 Experience
  • Google Experienced SWE First Round and Onsite: 1 Experience
All Google interview questionsSee Google hiring insights
Saved
class Solution {
    public int maxFriendshipHops(int n, int[][] friendships) {
        // Write your code here.
    }
}
Checking account…