Bloomberg LP Interview Question for Interns


Team: Financial Software Developer
Country: United States
Interview Type: Phone Interview




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

Take 2 stack lets say S1 and S2, 
Enqueue operation : Push in S1,
Dequeue Operation: 
if S2 is empty:
     pop every element from S1 and push into S2 one by one. 
pop from S2;

- Shashi January 25, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

There is a detailed solution in the entry "Queue from two stacks" at code.google.com/p/elements-of-programming-interviews/wiki/Programs.

I got this from the book titled "Elements of Programming Interviews: 300 Questions and Solutions".

- Anonymous January 25, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

stackoverflow.com/questions/69192/how-to-implement-a-queue-using-two-stacks

- 59.aditya January 26, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Algorithm:

Suppose we have two stacks (stackOne & stackTwo).

Insert new item in queue (Enqueue):
if stackOne is empty
Push this item on stackOne.
else
if stackTwo is empty
then pop possible items from stackOne and push on stackTwo
then push new item on stackOne.
else
print mesaage queue is full

Remove item from queue (Dequeue):
if stackTwo is not empty
then Pop item from stackTwo
else
if stackOne in not empty
Pop all Items from stackOne any place them on stackTwo in poped order.
Pop top item from stackTwo.
else
print message queue is empty.

- Bond January 26, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

i am coding only push and pop part

template<typename T>
void push(T x) {
   stck.push(x);
}
T pop() {
       if (s.empty()) throw error; 
       T ret = pop_bottom(s); 
}
T pop_bottom(stack<T> s)
{
       T top = s.top(); s.pop();
       if (s.empty()) return top;
       else {
                T ret = pop_bottom(s);
                 s.push(top);
                 return ret;  
       }
}

- Anonymous September 24, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

suppose the functions in stack include:
void empty();
element pop();
void push(element);

then

class queue{

stack stack1;
stack stack2;

void push_back(element)
{stack2.push(element);
}

void push_front(element)
{stack1.push(element);
}

element pop_front()
{ if (stack1.empty())
restack(stack1, stack2);
if (stack1.empty()) return null; else return stack1.pop();
}

element pop_back()
{ if (stack2.empty())
restack(stack2, tack1);
if (stack2.empty()) return null; else return stack2.pop();
}

void restack(stack stack1, stack stack2)
{while (!stack2.empty())
{element = stack2.pop();
stack1.push(element);
}


}

- xialijun0801 February 04, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

class queue_using_stack
{
	stack<int> st1, st2;
public:
	queue_using_stack()
	{

	}
	~queue_using_stack()
	{

	}
	void enqueue(int d)
	{
		st1.push(d);
	}	
	int dequeue()
	{
		int val = -1;

		if(st1.empty() && st2.empty())
			return val;

		else if(!st2.empty())
		{
			val = st2.top();
			st2.pop();
			return val;
		}
		else
		{
			while(!st1.empty())
			{
				st2.push(st1.top());
				st1.pop();
			}
			val = st2.top();
			st2.pop();
			return val;
		}
	}
};

- Siraj Memon December 30, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

class queue_using_stack
{
	stack<int> st1, st2;
public:
	queue_using_stack()
	{

	}
	~queue_using_stack()
	{

	}
	void enqueue(int d)
	{
		st1.push(d);
	}	
	int dequeue()
	{
		int val = -1;

		if(st1.empty() && st2.empty())
			return val;

		else if(!st2.empty())
		{
			val = st2.top();
			st2.pop();
			return val;
		}
		else
		{
			while(!st1.empty())
			{
				st2.push(st1.top());
				st1.pop();
			}
			val = st2.top();
			st2.pop();
			return val;
		}
	}
};

- Siraj Memon December 30, 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