Implemented my algorithm given at the interview itself.