Bloomberg Interview Question

Find the smallest two values in an array

Interview Answers

Anonymous

Jun 15, 2016

private static void smallestTwoValues(int[] a) { int one = a[0] = a[1] ? a[0] : a[1]; for(int i = 2; i < a.length; i++) { if(a[i] < one) { two = one; one = a[i]; } } System.out.println(one); System.out.println(two); }

Anonymous

Mar 13, 2015

Iterate through it and find the smallest two