Duolingo Interview Question

They asked me a medium question involving DFS with a few additional constraints