Pregunta de entrevista de Nutanix

Hard DSA problem which was tree+graph based question.