710

O mica problema pt cei curioshi :)

Mai in scurt e o intrebare dintr-un interviu tehnic."Assume you are given two fixed lists of numbers (arbitrary size, but you can assume the lists are large):a) Write a method that can efficiently find out if there is any element in the 2nd list that is an element of the first list.b) Describe some of the tradeoffs you can make with regards to memory and complexity."limbajul e java, deshi cred ca se poate de facut shi in alte limbaje... insa solutzia mea e in java...deci solutzia mea e urm:/** * Assume we need to find out if at least 1 element in 1st list is contained * in 2nd list */public boolean method(List l1, List l2){// group size, max// i think with this value we could play// a little moreint groupSize = 1000;// we will use hashmap, because it is faster than hashtableMap> m = new HashMap>(l2.size());// we will create 1t our container with grouped integersfor (Integer i : l2){int key = i / groupSize;List l = m.get(key);if (l == null){l = new ArrayList();m.put(i / groupSize, l);}l.add(i);}// System.out.println("We have " + m.values().size() + " groups");// now we will try to find at least one elementfor (Integer item : l1){// 1st we find the group for item, then we check if it is thereint key = item / groupSize;List l = m.get(key);if (l != null){// here we use what java gives usif (l.contains(item)){return true;}}}return false;}
0