Citadel Interview Question

A harder version of handshake problem