Google Interview Question

The question was a medium-hard graph-based problem, with a follow-up that built on the initial solution. I managed to code the solution and handle the follow-up within the time limit.