Cheapest Referral Chain to a Hiring Manager Within K Hops

Last updated: November 29, 2026

Quick Overview

Members can forward a referral request along directed edges, each with a cost (an effort score). Find the minimum total cost for a referral to travel from a source member to a target hiring manager using at most K edges, or -1 if impossible.

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

0

0

0 solved


Members can forward a referral request along directed edges, each with a cost (an effort score). Find the minimum total cost for a referral to travel from a source member to a target hiring manager using at most K edges, or -1 if impossible.

Warm introductions through the LinkedIn graph are bounded: people rarely forward beyond a couple of hops. The interviewer checks that you see plain Dijkstra fails with a hop limit and reach for bounded Bellman-Ford or Dijkstra over (node, hops) states.

What the Interviewer Expects
  • Explains why plain Dijkstra can discard a cheaper-but-longer path that a hop limit needs
  • Runs K rounds of Bellman-Ford relaxation using the previous round's distances
  • Copies the distance array each round to avoid using more than one new edge per round
  • Returns -1 when the target is unreachable within K edges
  • States O(K E) complexity and compares with state-space Dijkstra
Key Topics to Cover
Bellman-Ford
shortest path
hop constraint
weighted graph
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 does Dijkstra on (node, hops used) states compare?
  • What if costs can be negative but there are no negative cycles?
  • How would you return the actual chain of members?
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

Input: n members labeled 0..n-1, edges as (u, v, cost) with nonnegative cost, src, dst, and K = maximum edges allowed. Output: minimum cost or -1. src == dst costs 0....

High-Level Design

Bellman-Ford's i-th round computes the cheapest cost using at most i edges, as long as each round relaxes from a snapshot of the previous round. Run K rounds. The snapshot is the critical detail; rela...


Submit Your Answer
Markdown supported