Bloomberg LP Interview Question
Software Engineer / DevelopersI think the question means that there might be an unknown length stream(sequence) of numbers, which lie between 1 and 9.
In this case, something like array A[9] or array A[10] would not be a good idea as the length of the stream is unknown.
Hence, for me, a more correct answer would be a linked list as the length can be variable and would not have to be known at compile time.
Possible drawback of linked list would be that the data(number) we would like to store in each node would be far smaller (shorter in terms of bits needed to represent) than the pointers that would maintain the list.
Any suggestions to get around this issue?
I was asked this question: "You are getting stream of numbers between 1 to 9 (this may contain duplicates). You have to maintain these numbers in SORTED array (don't discard duplicates). Which is the best data structure you would use?"
Answer that I can think of is: link list, because array is bad idea as number of elements is unknown. With link list, insertions are faster so maintain a sorted link list and as you get a new number, insert it at a proper place in link list.
A balanced AVL tree or a red black tree wud be the best since the worst case insertion & lookup times are O(logn). But, extra time may be consumed for node rotation & tree balancing. Hashmap is a bad approach wherever sorting is required. Hashmap is good if we need to deal with duplicates & want O(1) access time. So, balanced binary is the best i can think of.
use array int a[9]. The number is the array index, a[index] represents how many of this number received so far.
- john May 16, 2009