iFIT interview question

Solve a medium level algo