Amazon Interview Question

graphs problem could not able to solve