Amazon interview question

Fix some bugs in the code to make it work.