LinkedIn interview question

a typical dfs question with complexity analysis