Apple Interview Question

Graph traversal question with optimization follow up