Amazon interview question

Graph related question and stack.