Amazon Interview Question

How would you implement a sparse array (key could be any integer, but only a few would be used) with limited memory.

Interview Answers

Anonymous

Sep 7, 2011

A hash table. A linked list is possible but random access isn't efficient

Anonymous

Sep 23, 2011

need to support efficient sequential read as well. b-tree

Anonymous

Feb 17, 2012

Hash table

Anonymous

Oct 19, 2011

Assuming sparse means many values are zero: have 2 arrays: one to store non-zero indexes, the other to store the values at the non-zero index. If queried number is not in the index set, return 0.