Amazon Interview Question

LC medium (graph problem LC M).