Pregunta de entrevista de eBay

Graph question from Leetcode top 100