Publicis Sapient Interview Question

basic algorithmic question, based on dfs.