Meta OA Interview Question: Minimum Steps in a Social Network

28 Views
No Comments

Given a social network, a start person, and an end person, find the minimum number of steps from person A to person B.

Example:

1 -> 2 -> 5
|    |    |
|    |    |
|    |    |
v    v    v
4 <- 3

Queries:

1, 5: 2
3, 1: 2
5, 3: -1

This problem asks for the shortest number of steps between two people in a social network. Model each person as a node and each connection as an edge, then use BFS to explore the graph level by level; the first time the end person is reached gives the minimum distance. If the graph is directed, respect edge direction, and return -1 when no path exists. It mainly tests graph modeling and BFS shortest-path fundamentals.

END
 0