Bloomberg LP Interview Question for Financial Software Developers


Country: United States
Interview Type: Phone Interview




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

A Deque is a doubly ended queue, that allows insertion and deletion at both the ends.This can be implemented by using a doubly linked list with all the operations in O(1).
Implementation can very easily be provided as follows by creating two extra empty nodes header and tailer pointing to each other at the start. Manipulate the pointers for adding and deleting.

- Achilles June 11, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Not so fast. From a reference at cplusplus dot com:

Deques may be implemented by specific libraries in different ways, but in all cases they allow for the individual elements to be accessed through random access iterators, with storage always handled automatically (expanding and contracting as needed).
...
Both vectors and deques provide thus a very similar interface and can be used for similar purposes, but internally both work in quite different ways: While vectors are very similar to a plain array that grows by reallocating all of its elements in a unique block when its capacity is exhausted, the elements of a deques can be divided in several chunks of storage, with the class keeping all this information and providing a uniform access to the elements. Therefore, deques are a little more complex internally, but this generally allows them to grow more efficiently than the vectors with their capacity managed automatically, specially in large sequences, because massive reallocations are avoided.

That doesn't sound too much like a doubly-ended linked list. Deques provide a random access iterator, but yet they're also not arrays; they split the storage into multiple chunks.

- Anonymous June 12, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I don't know about C++ but Java provides the same "java.util.Deque" in two classes, one using an array and the other using a linked list. Also, refer to Data Structure in Java by Goodrich and Tamasia, they have also provided the implementation using LinkedList.
LinkedList may not be the ideal candidate for Deque but is definitely one of the best amongst the known data structures.

- Achilles June 12, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I see, Java also has an interface called Deque, which apparently is different from what's called a deque in C++. I've devleoped far more Java than C++, and yet I've never really heard much mention of these in Java, though I hear about them in C++ contexts. I suppose the thing to ask would be which deque we're discussing. Reviewing the Javadoc, I agree that Deques in Java seem to just be double ended queues with nothing too fancy. The array implementation is probably implemented as a circular buffer.

- Anonymous June 14, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

So then the thing to ask would be whether one wants the array or linked list implementation.

- Anonymous June 14, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

So...it seems no one knows what a deque actually is, odd as it sounds. Someone asked a question on this on StackOverflow and people there couldn't come to a consensus about what sort of data structure a deque would have to be to satisfy the specification's requirements.

- Anonymous June 11, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <iostream>
#include <deque>
using namespace std;

int main()
{
// Stack behavior using a STL deque
deque<int> q;
try {
if ( q.empty() )
{
cout << "Deque is empty" << endl;
}

// Push elements
q.push_back(100);
q.push_back(200);
q.push_back(300);

// Size of queue
cout << "Size of deque = " << q.size() << endl;

// Pop elements
cout << q.back() << endl;
q.pop_back();
cout << q.back() << endl;
q.pop_back();
cout << q.back() << endl;
q.pop_back();
}
catch (...) {
cout << "Some exception occured" << endl;
}

// Queue behavior using a STL deque
deque<int> q1;
try {
if ( q1.empty() )
{
cout << "Deque is empty" << endl;
}

// Push elements
q1.push_back(100);
q1.push_back(200);
q1.push_back(300);

// Size of queue
cout << "Size of deque = " << q1.size() << endl;

// Pop elements
cout << q1.front() << endl;
q1.pop_front();
cout << q1.front() << endl;
q1.pop_front();
cout << q1.front() << endl;
q1.pop_front();
}
catch (...) {
cout << "Some exception occured" << endl;
}
}

Deque is empty
Size of dequeue = 3
300
200
100
Deque is empty
Size of dequeue = 3
100
200
300

- Raju B July 22, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <iostream>

using namespace std;


template <class T>
class Node{
  
    public:
        Node();
        Node(T item):next(NULL),prev(NULL),data(item){ }
        ~Node(){ prev = next = NULL; }   
 
    T data;
    Node<T> *next;
    Node<T> *prev;
};

class DequeEmptyException{
    public:
        DequeEmptyException(){
            cout << "Deque is empty" << endl;        
        }

        ~DequeEmptyException(){ }
};

template <class T>
class Deque {
    public:
        Deque():front(NULL),rear(NULL),count(0){}

        int AddFront(const T element);
        T RemoveFront();
        int AddBack(const T element);
        T RemoveBack();

        int Size() const;

        T Front() const;
        T Back() const;

        int IsEmpty() const;

        private:
            Node<T> *front;
            Node<T> *rear;
            int count;

};

template <class T>
int Deque<T>::IsEmpty() const{
    return count == 0 ? 1 : 0;
}

template <class T>
int Deque<T>::Size() const{
    return count;
}

template <class T>
T Deque<T>::Front() const{
    if(IsEmpty())
        throw new DequeEmptyException();

    return front->data;
}

template <class T>
T Deque<T>::Back() const{
    if(IsEmpty())
        throw new DequeEmptyException();

    return rear->data;
}

template <class T>
int Deque<T>::AddFront(const T element){

    Node<T> *newNode = new Node<T>(element);

    cout << "item: " << newNode->data << endl;    

    if(IsEmpty()){
        front = rear = newNode;
        ++count;
        return 0;
    }
    newNode->next = front;
    front->prev = newNode;
    front = newNode;
    ++count;

    return 0;
}

template <class T>
T Deque<T>::RemoveFront(){

    if(IsEmpty()){
        throw new DequeEmptyException();
    }
    
    T retVal = front->data;
 
    Node<T> *tmp = front;
    if( front->next != NULL){
        front = front->next;
        front->prev = NULL;
    }
    else{
        front = NULL;
    }
    count--;
    delete tmp;
    tmp = NULL;

    return retVal;
}

template <class T>
int Deque<T>::AddBack(const T element){

    Node<T> *newNode = new Node<T>(element);

    if(IsEmpty()){
        front = rear = newNode;
        ++count;
        return 0;
    }
    // append to the list
    rear->next = newNode;
    newNode->prev = rear;
    rear = newNode;    
    count++;
}

template <class T>
T Deque<T>::RemoveBack(){

    if(IsEmpty()){
        throw new DequeEmptyException();
    }

    T retVal = rear->data;
  
    // delete the back node
    Node<T> *tmp = rear;

    if( rear->prev != NULL){
        rear = rear->prev;
        rear->next = NULL;             
    }
    else{
        rear = NULL;
    }
    count--;
    delete tmp;
    tmp = NULL;

    return retVal;
}

- Instigo August 21, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Create an array of proper size, and then start from the middle of array. Push_back : push after the current elements; push_front: push in front the the current elements.

When the array is not big enough for the front: create another array; its index 0 is the new front, and when ever we do push_front(), the new elements is put at the end of the new array.

When the array is not big enough for the deque back: create another array; new push_back() put the new element in the end of the new array.

in short, there are the front-arrays and the back-arrays, and the middle arrays.

- Haoju June 30, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Thanks

- GA April 14, 2015 | Flag Reply


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