Hinge interview question

A common cs algo problem