← LinkedIn Interview Insights

LinkedIn·Software Engineer·Technical Phone Screen·Intermediate

Intermediate
May 2026

Summary

LinkedIn coding screen, graph traversal problem on a social network model. Pretty much a BFS question dressed up in their own domain language, which was a nice touch but didn't change what you had to do.

Questions Asked (1)

Q1

Given a Candidate interface with an id and a getConnections() method, implement a function that returns the shortest path length (in hops) between two Candidate nodes in a connection graph. Return -1 if no path exists.

Algorithms & Data Structures
Author's notes

The social network framing is cute but once you see it's just shortest path on an unweighted graph, it's BFS and nothing else.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Use BFS from the source node, tracking visited nodes and hop count level by level. Stop when the target is found, returning the current hop count; if the queue empties, return -1.

Pro tip: Mention that BFS is optimal for unweighted graphs and handle edge cases like source equals target (return 0) and null inputs. Also, clarify whether the graph is directed or undirected, as it affects traversal.

1. Clarify requirements and edge cases

Confirm if the graph is directed or undirected, and discuss edge cases such as source == target, null nodes, or disconnected graphs.

2. Choose BFS and initialize data structures

Select BFS for shortest path in unweighted graphs. Initialize a queue with the source node, a visited set, and a distance variable (or store distance in queue).

3. Traverse level by level

While the queue is not empty, process all nodes at the current level, incrementing hop count. For each node, enqueue unvisited neighbors and mark them visited.

4. Check for target and return result

If the target is found during traversal, return the current hop count. If the queue empties without finding the target, return -1.

5. Analyze complexity and optimize

State time complexity O(V+E) and space O(V). Mention potential optimizations like bidirectional BFS if needed.

Key Points to Mention

  • BFS guarantees shortest path in unweighted graphs
  • Use a visited set to avoid cycles and redundant work
  • Track hop count by levels (e.g., using queue size or distance array)
  • Handle edge cases: source == target (return 0), null inputs, disconnected nodes
  • Time and space complexity: O(V+E) time, O(V) space
  • Consider bidirectional BFS for performance improvement in large graphs

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.