Align Technology interview question

Handshake problem from leetcode .