Bloomberg L.P. Interview Question: Given four arrays containing ... | Glassdoor

Interview Question

Financial Software Developer Interview Duluth, MN

Given four arrays containing any number of random

  intergers, find the intersection of the four arrays. Asked to improve the time complexity till they were satisfied.
Answer

Interview Answer

1 Answer

0

private static int intersection(int[][] all) {

        Map map = new HashMap();

        for (int[] is : all) {
            for (int i : is) {
                if (map.containsKey(i)) {
                    map.put(i, map.get(i) + 1);
                } else {
                    map.put(i, 1);
                }
            }
        }

        for (Entry entry : map.entrySet()) {
            if(entry.getValue() == 4)
                return entry.getKey();
        }

        return -1;
    }

Anonymous on Jun 17, 2016

Add Answers or Comments

To comment on this, Sign In or Sign Up.