Apple interview question

Graph traversal question with optimization follow up