Build referral chains and detect cycles | Block (Square) Interview Question