Amazon interview question

A Question on Data Structures (Hash Map) which i had to code with optimal time and space complexity.