Publicis Sapient interview question

basic algorithmic question, based on dfs.