Amazon Interview Question
Software Engineer / DevelopersCountry: India
Interview Type: In-Person
I agree!
The more important factor to consider here is that how are the number stored and what is the retrieval methods used. Quite possible having a cache makes is little faster.
Also, I think this question seems to be like coming up with an algo that works on multiple groups of data at a time.
public class CountRepeatativeNo {
int a[] = {1, 4, 1, 5, 2, 2, 4, 3, 4, 1};
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
public void test()
{
for(int i = 0; i < a.length ; i++)
{
if(map.containsKey(a[i]))
{
map.put(a[i], map.get(a[i])+1 );
}
else
{
map.put(a[i], 1);
}
}
Iterator i=map.keySet().iterator();
Iterator j=map.keySet().iterator();
while(i.hasNext() && j.hasNext())
{
System.out.println("for "+j.next()+":"+map.get(i.next()));
}
}
public static void main(String args[])
{
CountRepeatativeNo h=new CountRepeatativeNo();
h.test();
}
}
I think this type of questions are more inclined toward your ability to "ask" and refine the requirements. For instance, I'd ask:
- Herman July 16, 2012- Where are the numbers coming from?
- What is the range?
- What are the memory/time constraints?
If the numbers are ages or binary (just 1 an 0) this could easily be done with an array.