Goldman Sachs Interview Question

Find the second smallest integer in an array. Do not worry about duplicates.

Interview Answers

Anonymous

Apr 6, 2018

I sorted the array and returned the second element.

3

Anonymous

Apr 26, 2018

You need to use Hoare partition

1

Anonymous

Jun 7, 2018

void secondSmallest(int arr[], int arr_size) { int i, first, second; first = second = arr[0]; for (i = 0; i < arr_size ; i ++) { if (arr[i] < first) { second = first; first = arr[i]; } else if (arr[i] < second) second = arr[i]; } printf("The second smallest element is %d", second); }

Anonymous

Dec 4, 2019

def second_smallest_num(arr): new_arr = {"smallest": arr[0], "second": arr[1]} pivot = arr[0] for i in arr: if i new_arr["smallest"]: new_arr["second"] = i print new_arr arr = [200,-100,-3,-2,0,4,-3,-1] second_smallest_num(arr)

Anonymous

Jul 25, 2018

Do not sort the array that’ll give you O(nlogn) runtime, you can do this in O(n) runtime with one pass through the array

1