LinkedIn Interview Question

a typical dfs question with complexity analysis