Compute Connection Degree Between Two Members Up to Third

Last updated: November 29, 2026

Quick Overview

Given an undirected connection graph as an adjacency map and two member ids, return 1, 2, or 3 for their connection degree, or -1 if they are farther apart than three hops or unreachable.

LinkedIn
Coding & Algorithms
Software Engineer
LinkedIn
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Medium

0

0

0 solved


Given an undirected connection graph as an adjacency map and two member ids, return 1, 2, or 3 for their connection degree, or -1 if they are farther apart than three hops or unreachable.

The 1st, 2nd, 3rd badge on every LinkedIn profile is the canonical graph question in its loop. Interviewers look for bounded BFS, ideally bidirectional, and an understanding of why fan-out makes naive BFS expensive on a social graph.

What the Interviewer Expects
  • Uses BFS rather than DFS for shortest hop count
  • Bounds the search depth at three
  • Expands from both ends, choosing the smaller frontier each step
  • Handles same member and missing ids explicitly
  • Explains fan-out cost: 500 connections cubed is huge, bidirectional is roughly two times 500 squared
Key Topics to Cover
BFS
bidirectional search
social graph
bounded depth
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • How would you precompute or cache second-degree networks for fast lookups?
  • How does this work when the graph is sharded across machines?
  • How would you return the mutual connections that link them at degree 2?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements

Graph: dict[member] -> set(connections), undirected. Degree is the shortest hop count. LinkedIn only shows up to 3rd degree, so return -1 beyond that. If a == b, return 0. Missing members return -...

High-Level Design

Bidirectional BFS: keep frontier and visited sets from each side. Each round, expand the smaller frontier one level and check intersection with the other side's visited set. The total depth is the sum...


Submit Your Answer
Markdown supported