Fragen im Vorstellungsgespräch bei Bloomberg L.P.: Given a list of integers A an... | Glassdoor.de

Frage im Vorstellungsgespräch

Senior Software Developer-Vorstellungsgespräch New York, NY (Vereinigte Staaten von Amerika)

Given a list of integers A and an integer k, return the

  number of unique pairs of integers in A that sum to k. For example, if A=[1 2 5 5 10 8 2] and k=10, the answer is 2, arrived as 5+5 or 2+8. There were 15 tests for this question, with at least the last two timing out, given that the implementation was not as efficient as possible.
Antwort

Antwort im Vorstellungsgespräch

5 Antworten

1

ar = [1,2,5,5,10,8,2]
def f(target):
  target = 10
  tic = time.time()
  comb1 = [(k,v) for k,v in combinations(ar,2)] #generate combinations
  comb1 = sorted(map(lambda x:sorted(x),comb1))
  comb1 = [k for k,_ in groupby(comb1)]
  return len(filter(lambda x:sum(x)==target,comb1))

id2k am 05.01.2017
0

// c++
auto main() -> int {

    list A{ 1,2,3,4,5,9,5 };
    int N{ 10 };

    int res = 0;
    while (1 < A.size()) {
        auto a = *A.begin();

        A.pop_front();

        auto pos = find(A.begin(), A.end(), N - a);

        if (pos != A.end()) {
            A.erase(pos);
            ++res;
        }
    }

    cout << res;

    getchar();
}

Anonym am 11.02.2017
0

int solution(int[] A, int k) {
    Set remainders = new HashSet();
    int pairs = 0;
    for (int i=0; i< A.length; i++) {
        int remainder = k - A[i];
        if (remainders.remove(remainder)) {
            pairs++;
        } else {
            remainders.add(A[i]);
        }
    }
    return pairs;
}

Java am 30.05.2017
0

public static void main(String[] args) {
        int[] a={1, 2, 5, 5, 10, 8, 2};
        int target = 10;
        Arrays.sort(a);
        int i = 0, j = a.length-1;
        while(i i && a[j] == a[j+1]) j--;
                while(j > i && a[i] == a[i-1]) i++;
            }else if(sum i && a[j] == a[j+1]);
            }
        }
    }

Anonym am 05.11.2017
0

public class UniquePair {
    private static int PAIR_COUNT = 0;
    public static void main(String args[]){
        int A[] = new int[]{1,2,5,5,10,8,2,9};
        int K = 10;
        countPairs(A, K);
        System.out.println(PAIR_COUNT);
    }

    public static void countPairs(int[] A, int K){
        Map map = new HashMap();
        for (Integer i : A){
            if (map.containsKey(Math.abs(K-i))) {
                int num = K-i > 0 ? K-i : i-K;
                int val = map.get(num);
                val--;
                if (val==0) {
                    map.remove(num);
                } else {
                    map.put(num, val);
                }
                System.out.println("Pair : " + (num) + " , " + i);
                PAIR_COUNT++;
            } else {
                if (map.containsKey(i)) {
                    int val = map.get(i);
                    val++;
                    map.put(i, val);
                } else {
                    map.put(i, 1);
                }
            }

        }
    }
}

Anonym am 12.06.2018

Antwort oder Kommentar posten

Um dies zu kommentieren, bitte anmelden oder Konto anlegen.