Amazon Interview Question
Country: India
if(newValue > firstPlace) {
firstPlace = newValue;
firstPlaceNum = num;
}
I suppose this happens when second place occured the same number of times as the first place, and you updated the firstPlaceNum to be the old second place number, but second place also points to this same number. Did I misunderstand the code somewhere?
Questions like this must be answered by giving various approaches:-
1> As suggested, Hashtable with key as value from array and value being the count.
Then sort the hashtable.(Min Heap of size 3 , iterate thorugh all the keys and in accordance through count put them in heap)
2>similarly Balanced BST with frequency as additional value in node. then inorder traversal and putting values in heap
Bounded heap concept. Basically at each frequency you check min of the heap and then if your frequency happens to be greater than min you insert it into heap(b4 delete the min). So this way you will always have the maximum 3 .
If frequency is just an additional value and not the key value, the in-order traversal will take place on the key value and not the frequency
@aztec It has to be a min heap of 3 highest ferq element so that you can compare the next element's from the top of min heap and decide if it belongs to the min heap(of highest freq elements) or not(in which case you can just move to next element)
However you can avoid all this post processing of creating heap by tracking which 3 elements has the largest count while creating the hash itself which has been show in code by Will_In_Seattle's post
if the numbers are not very large we can use an array of length = the max of the numbers.
for each element in the given array increment the value from the position "element" in the new array. Now go 3 times and pick the max value from our array, putting an invalid value in its place and memorizing the position of the max.
return those 3 positions.
I think my code could solve some what of this problem.
public static void main(String args[]){
int RAM[] ={20,8,3,7,8,9,20,6,4,6,20,8,20};
for(int i=0;i<RAM.length;i++){
int count = 0;
for(int j=0;j<RAM.length;j++){
if(RAM[i]==RAM[j]){
count = count+1;
}
}
if(count >= 2){
System.out.println(RAM[i]+ " :) " + count + " :) ");
}
}
}
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
public class Repeat {
public static void main(String args[]){
final int RAM[] = {20,8,3,7,8,9,20,6,4,6,20,8,20};
topRepeat(RAM);
}
public static int[] topRepeat(int input[]){
int times[] = {0,0,0};
int output[] = {0,0,0};
LinkedList<Integer> list = new LinkedList<Integer>();
for(int i : input){
if(list.contains(i))
continue;
list.add(i);
}
HashMap<Integer, Integer> hashMap =
new HashMap<Integer, Integer>();
for(int i : input){
if(hashMap.containsKey(i))
hashMap.put(i, hashMap.get(i) + 1);
else
hashMap.put(i, 1);
}
Iterator<Integer> it = list.iterator();
while(it.hasNext()){
int i = it.next();
int time = hashMap.get(i);
if(time > times[2]){
if(time > times[1]){
if(time > times[0]){
times[2] = times[1];
times[1] = times[0];
times[0] = time;
output[0] = i;
continue;
}
times[2] = times[1];
times[1] = time;
output[1] = i;
continue;
}
times[2] = time;
output[2] = i;
}
}
for(int i = 0; i < 3; i++)
System.out.printf("%d is repeated %d times\n", output[i], times[i]);
return output;
}
}
- Will_In_Seattle January 08, 2013