Final Code for the interview question