Bloomberg LP Interview Question
Financial Application EngineersCountry: United States
Interview Type: Phone Interview
Array is not a nice way to store this, what if a record gets deleted in between somewhere, algorithm goes bust. Linked list (preferrably a doubly in case have to handle past as well next set of records). Doesn't need to be a stack or queue, but for this particular question a stack i more preferred, since last 60 sec data is to be referred, as and when an object is added, that becomes the current timestamp (which needs to be popped) and from there 60 sec backward. So as the item is added, pop that and count 60 sec backwards. If the question was sum of next 60 items, then that kinda points towards a queue. But since we would not be able to answer both these questions together by using a stack or queue, simply implementing a doubly linked list would serve the purpose.
I think deque STL container, and only keep last 60 seconds items as "a0" said
Double-ended queues are sequence containers with dynamic sizes that can be expanded or contracted on both ends
they allow for the individual elements to be accessed directly through random access iterators
The complexity (efficiency) of common operations on deques is as follows:
Random access - constant O(1)
Insertion or removal of elements at the end or beginning - constant O(1)
so you can insert the new items at the end of the deque and remove the exceeded items from the beginning
and because the Random access constant O(1) you can count the 60 seconds items very fast with complexity O(n)
if the stream if items is in time order. queue could be used to store the item, and only keep last 60 seconds items
- a0 November 24, 2015