Google interview question

a medium level leetcode graph problem