Amazon Interview Question

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