Maximum Friendship Hops
Updated
Problem statement
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[][]) → intExamples
Example 1
n = 5friendships = [[0,1],[1,2],[2,3],[3,4]]return = 4The endpoints need four links; every other pair needs at most four.
Example 2
n = 4friendships = [[0,1],[1,2],[2,3],[3,0]]return = 2Opposite 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 = 1Every distinct pair has a direct friendship.
Constraints
- For this exercise, assume
1 <= n <= 300. 0 <= friendships.length <= 2000and 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