TEST ENGINE

Coding Practice Sprint

45:00
Submit
2023AlgoGraph Traversal
8 min
MediumCODE2 marks

Implement BFS distance

Write a function shortest_distance(graph, source, target) that returns the number of edges in the shortest unweighted path. Return -1 if unreachable.

input: {0:[1,2],1:[3],2:[3],3:[]}, 0, 3 => output: 2
input: {0:[1],1:[],2:[0]}, 0, 2 => output: -1
Live score
0/7