Question d’entretien chez Goldman Sachs

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

Réponses aux questions d'entretien

Utilisateur anonyme

6 avr. 2018

I sorted the array and returned the second element.

3

Utilisateur anonyme

26 avr. 2018

You need to use Hoare partition

1

Utilisateur anonyme

7 juin 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); }

Utilisateur anonyme

4 déc. 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)

Utilisateur anonyme

25 juil. 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