Amazon interview question

follow up union find algorithm