Two Sigma interview question

Friend circle question leetcode