Amazon Interview Question

What data structure to use to solve the problem.