Goldman Sachs Interview Question

Return the second-smallest element of a list.

Interview Answers

Anonymous

Oct 27, 2017

This can be done in following ways - Using collections: Collections.min(list); - Simple iteration: for (int i : array){ min = min < i ? min : i; }

Anonymous

Dec 4, 2017

Put in a max heap, pop the max, the top is now the second max.

Anonymous

Dec 5, 2017

#Python3 lst=[8,7,1,2,3,4,5] sorted(lst)[-2] #output is 7

Anonymous

Jan 4, 2018

pair a = (v[0] a.second) continue; if (*it < a.first) { a.second = a.first; a.first = *it; } else if (*it < a.second) { a.second = *it; } } return a.second;