EY interview question

Leetcode hard question based on graph