Amazon Interview Question

Find the first common element in two sets

Interview Answer

Anonymous

Oct 15, 2009

I first asked whether the sets were ordered, whether they could be ordered, and what kind of a structure they were stored in. We discussed these questions and then I basically said that you needed to order each list and and alternately step through them so we step through list a until its first value is more than the first of list b, then step through list b until its first value is more than the first of list a. He then asked if there were any data structures that could improve the complexity and since the complexity of my solution was linear I said that I didn't know of any. What he was trying to get at is that if you replace one of the lists with a heap run time can be reduced by half in the best case.