eBay

## Sollicitatievraag

Sollicitatiegesprek voor de functie Software QA Engineer

-

# how to find the closest 2 number in an array of unique positive number

## Antwoorden op sollicitatievragen

5 antwoorden

7

One solution is to sort the array, then iterate through all neighboring pairs to find the pair with smallest difference. This is O(NlogN) solution. If the integers are bounded, you may be able to sort in O(N) time for an O(N) solution.

Anoniem op

0

public static int[] find2Closest(int[] myArray) { assert(myArray.length >= 2): "myArray needs to be larger than 2"; Arrays.sort(myArray); //quickSort int indexMin1 = 0; int indexMin2 = 1; int min = myArray[indexMin1] - myArray[indexMin2]; for(int k = 0; k < myArray.length-1; k++) { int difference = Math.abs(myArray[k] - myArray[k+1]); if(difference < min) { min = difference; indexMin1 = k; indexMin2 = k+1; } } int[] values = new int[2]; values[0] = myArray[indexMin1]; values[1] = myArray[indexMin2]; return values; }

Batman op

0

public class ClosestTwoNumber { public int[] findClosest2Number(int[] arr){ if(arr == null || arr.length diff) { min = diff; num1 = arr[i - 1]; num2 = arr[i]; } } if (num1 == -1 || num2 == -1) { throw new IllegalArgumentException(); } int[] result = new int[2]; result[0] = num1; result[1] = num2; return result; }

Gokhan A. op

0

public class ClosestTwoNumber { public int[] findClosest2Number(int[] arr){ if(arr == null || arr.length diff) { min = diff; num1 = arr[i - 1]; num2 = arr[i]; } } if (num1 == -1 || num2 == -1) { throw new IllegalArgumentException(); } int[] result = new int[2]; result[0] = num1; result[1] = num2; return result; }

GA op

0

Anoniem op

## Voeg antwoorden of opmerkingen toe

Meld u aan of registreer u om hier een opmerking over te maken.

## Carrières bij eBay

eBay is a global commerce platform leader. eBay is the world’s most vibrant marketplace for discovering great value and unique...Meer

• Culture
• Benefits
• Our People
• Global Impact
Hier krijgt de werkgever de kans om te vertellen waarom u bij zijn of haar bedrijf moet komen werken. De informatie wordt gegeven vanuit werkgeversperspectief.