Nokia interview question

Coding question on DFS and Stack