Amazon interview question

Medium/Hard level graph traversal Leetcode Question