Pergunta de entrevista da empresa Goldman Sachs

Find the second smallest on array.

Respostas da entrevista

Sigiloso

4 de jun. de 2018

That's a good solution but it takes N Log N time and Log N space (because sort uses merge sort most of the time AFAIK). imo, a better solution would be to iterate the array for a min value, then iterate it again remembering the previous min index so you can ignore it. This solution would be 2n time complexity and constant memory. I'm making an assumption that all integers are unique in this case as [0, 0, 1] for my algorithm would return 0 but on the second index here. Even in the above solution this would be a problem. My algorithm can be adopted to remember the previous val if this matters and only return a different min value.

5

Sigiloso

27 de mar. de 2018

public static int secondSmallestonArray(int [] a){ Arrays.sort(a); return a[1]; } or if you can't use a java sorting library sort the array and return index 1 or second element of an sorted array.