## Interview Question

• 0

Country: United States

Comment hidden because of low score. Click to expand.
1
of 1 vote

``````function find (N, list) {
var map = {}
for (var i = 0; i < list.length; ++i) {
if (list[i] <= N) {
map[list[i]] = i
}
}
console.log(map)
closestD = Infinity
closestPair = [null, null]
for (var key in map) {
if (({}).hasOwnProperty.call(map, key)) {
var m = key
var n = N - m
var p = map[n]
console.log('n,m', n,m)
if ('' + n === m) {
// Ignore things like the fact that 4 pairs with itself
// to produce 4,4 d=0 ; a more robust implementation handles this
} else if (p !== undefined) {
var d = Math.abs(map[m] - p)
console.log(m, p, N, n, d)
if (d < closestD) {
closestD = d
closestPair = [m,n]
}
}
}
}
console.log(closestD, closestPair)
}

find (8, [1,5,3,6,4,2])``````

Comment hidden because of low score. Click to expand.
0
of 0 vote

``````rivate static int[] getClosestSum(int target[], int sum, int diff, int index, int[] pair, int nextindex) {

// int [] array = { 1, 5, 3, 6, 2};
int[] nextPair = null;
if (target.length == index || target.length == nextindex) {
return pair;
}

nextPair = getClosestSum(target, sum, diff, index, pair, nextindex + 1);

if (target[index] + target[nextindex] == sum) {

int currentMin = target[index] - target[nextindex];
int prevMin = pair[0] - pair[1];

if (currentMin == Math.min(currentMin, prevMin) && currentMin >= 0) {
pair[0] = target[index];
pair[1] = target[nextindex];
}

}

// nextPair = getClosestSum(target, sum, diff, index+1, pair,
// nextindex+1);

return pair;
}

public static void main(String[] args) {

int[] array = { 1, 5, 3, 6, 2 };
int[] pair = { Integer.MAX_VALUE, 0 };
int[] result = null;
int index = 0;
for (int num : array) {
result = getClosestSum(array, 8, 0, index++, pair, 1);
}

System.out.println(result);

// given 8 = {5,3} and not {6,2}

}``````

Comment hidden because of low score. Click to expand.
0
of 0 vote

``````rivate static int[] getClosestSum(int target[], int sum, int diff, int index, int[] pair, int nextindex) {

// int [] array = { 1, 5, 3, 6, 2};
int[] nextPair = null;
if (target.length == index || target.length == nextindex) {
return pair;
}

nextPair = getClosestSum(target, sum, diff, index, pair, nextindex + 1);

if (target[index] + target[nextindex] == sum) {

int currentMin = target[index] - target[nextindex];
int prevMin = pair[0] - pair[1];

if (currentMin == Math.min(currentMin, prevMin) && currentMin >= 0) {
pair[0] = target[index];
pair[1] = target[nextindex];
}

}

// nextPair = getClosestSum(target, sum, diff, index+1, pair,
// nextindex+1);

return pair;
}

public static void main(String[] args) {

int[] array = { 1, 5, 3, 6, 2 };
int[] pair = { Integer.MAX_VALUE, 0 };
int[] result = null;
int index = 0;
for (int num : array) {
result = getClosestSum(array, 8, 0, index++, pair, 1);
}

System.out.println(result);

// given 8 = {5,3} and not {6,2}

}``````

Comment hidden because of low score. Click to expand.
0
of 0 vote

In ZoomBA:

``````/*
To solve this :
1. Create a map with value as key, and index as value.
2. Iterate the list with testing sum (S) S - item is in the map or not.
2.1. If it is,
2.1.1 and the difference in position is
smaller than previous min store as min
2.1.2 Ignore
2.2. If it is not continue to [2]
*/

def solution( arr, S ){
// a map of key ( item ) -> item index
map = dict(arr) -> { [ \$.o , \$.i ] }
min = [ -1,-1, num('inf') ] // store +infinity as minimum
fold ( arr , min ) -> {
diff = S - \$.o
if( diff @ map &&  map[diff] != \$.i && \$.p.2 > #|\$.i - map[diff]| ){
\$.p = [ \$.i , map[diff] , #|\$.i - map[diff]| ]
}
\$.p // returns
}
}
arr = [1,4,5,3,6,4,2]
#(i,j) = solution( arr , 8 )
printf( '%d,%d\n', arr[i], arr[j] )``````

Comment hidden because of low score. Click to expand.
0
of 0 vote

``````from itertools import combinations

def closet_sum(nums, n):
print min([(abs(i-j), i, j) for (i,j) in combinations(nums,2) if i+j == n], key = lambda x: x[0])[1:]``````

Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

### Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

### Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.