Amazon Interview Question

Medium difficulty coding Q about connected components.