Ebay Interview Question for Software Engineer Interns


Country: United States
Interview Type: Phone Interview




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

in terms of MR it could be

public List<T> findTopK(List<T> inputData, int k) {
		List<T> result = new ArrayList<T>();
		// key - inputDataObject, value - its count
		Map<T, Integer> map = mapInput(inputData);
		// sort map by count values
		List<Map.Entry<T, Integer>> sortedEntries = sortMapByValue(map);
		// check top size
		if (k > sortedEntries.size())
			k = sortedEntries.size();
		// get top
		for (int i = sortedEntries.size() - k; i < sortedEntries.size(); i++) {
			result.add(sortedEntries.get(i).getKey());
		}
		return result;
	}

	public Map<T, Integer> mapInput(List<T> inputData) {
		Map<T, Integer> map = new HashMap<T, Integer>();
		for (T t : inputData) {
			if (map.containsKey(t)) {
				int count = map.get(t);
				map.put(t, ++count);
			} else {
				map.put(t, 1);
			}
		}
		return map;
	}

	public List<Map.Entry<T, Integer>> sortMapByValue(Map<T, Integer> map) {
		List<Map.Entry<T, Integer>> sortedEntries = new ArrayList<Map.Entry<T, Integer>>();
		sortedEntries.addAll(map.entrySet());
		Collections.sort(sortedEntries, new Comparator() {
			public int compare(Object o1, Object o2) {
				return ((Comparable) ((Map.Entry) (o1)).getValue())
						.compareTo(((Map.Entry) (o2)).getValue());
			}
		});
		return sortedEntries;
	}

- Oleg G April 22, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

A possible approach:

1. Build a map of counters whose keys are the elements and the values are the appearance counters of each element.
2. Create an array of the map's entries - the entries are of the form (key,value) where key is an element and value is the counter of the number of times the element appeared.
3. Use the median of median algorithm to find the k-th highest entry according to value. This means that comparison between two entries is made according to their value fields (the counters).
4. Iterate over the array and return all the keys of the entries that have a higher value (counter) than the median counter we found in step 3. There should be at least k such elements (maybe more in case there are several elements whose counter is the median the counter - in this case we can either return all of them or just the first k elements we find).

Complexity: O(n) amortized (because of the counter map) run-time complexity (median of median is O(n) worst case). Space complexity is O(n) as well.

- IvgenyNovo April 22, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

Assuming that there is an infinite stream of numbers and your are supposed to tell the k most occurring numbers, I am giving you an approach.

1. Maintain a Linked List with decreasing order of frequency. Each node of linked list will have the number and its frequency.
2. Maintain a hash map with key as number and value as the pointer to the node in the linked list.
3. If a new number comes, add it to the tail of the linked list, maintaing the hash map.

The cost of maintaining the list for every number is O(1).
Reason:
1. When the list is empty - add a node O(1)
2. When node is not in list - add a node O(1)
3. when frequency of a node changes, it moves before the previous node - O(1)

The cost of returning the top occurred (k) numbers - O(k) - returning the first k numbers from the linked list.

- Sanyam Jain May 04, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

1. Create a frequency array of size (n + 1); where n is the maximum element in input array
2. For every element i, in the input array, increment the ith index in the frequency array by
3. Sort the frequency array in reverse order (decrementing).
4. The first k elements are the most frequent elements in the input array.

int[] array = new int[] { 1, 2, 1, 1, 3, 4, 5, 3, 3, 3, 3, 3, 5, 6, 7,
				2, 9, 7, 5 };

		int k = 3;

		Integer frequency[] = new Integer[10];
		Arrays.fill(frequency, 0);

		for (int i = 0; i < frequency.length; i++) {
			frequency[array[i]] += 1;
		}

		Arrays.sort(frequency, new Comparator<Integer>() {
			public int compare(Integer o1, Integer o2) {
				if (o1.intValue() < o2.intValue()) {
					return 1;
				} else if (o1.intValue() > o2.intValue()) {
					return -1;
				} else {
					return 0;
				}
			};
		});

		for (int i = 0; i < k; i++) {
			System.out.println(array[frequency[i]]);
		}

- SumitGaur April 21, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Use priority queue data structure ie. Max-Heap tree where key is frequency of the element.

- khunima April 21, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Can you provide the code ?

- Anonymous June 08, 2014 | Flag


Add a Comment
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.

Learn More

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.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More