Facebook Interview Question for Software Engineer / Developers






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

I think we will need two stacks to implement queue :-
stack has operations top(),empty(),pop(),add()

lets take two stacks s1 and s2
keep a variable FRONT
nOw :-

1) Create queue :- create s1 and s2 and FRONT = NULL and REAR = NULL;
2) Enqueue (e) :- -----------O(1) 
                  if( s1.empty() ) then FRONT = e
                      REAR = e;
                      s1.add(e)
3) Dequeue() :-         ---------------------------------O (Num. elements)
                        if ( s1.empty() then UNDERFLOW 
                        else {
                             while( !s1.empty() )
                                     s2.add( s1.pop());
                             s2.pop();
                             if( !s2.empty() ) FRONT = s2.top(); Else REAR = NULL;
                             while( !s2.empty() ) s1.add( s2.pop() );
                        }

4) Front() :- return FRONT  ---- O(1)
5) Rear() :- return REAR --------O(1)

- Aditya October 20, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Dequeue can be optimized further. There is no need to pop the s2 back to s1. Just check s2 for elements first before accessing s1.

Dequeue()
{
if (s2.empty()) {
while (!s1.empty()) {
s2.push_back(s1)
}
}
if (s2.empty()) UNDERFLOW;
return s2.pop()
}

- Anon May 17, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

then it will cause problems to do enqueue again.

- sophia August 30, 2012 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

Using two stacks. One for adding, second for removing items.
Befor add/remove check where are items and pop them if needed.

O(n) for enqueue, O(n) for dequeue in worst case. Sequential adding - O(1).
Sequential dequeue - O(1).

Worst case for switching from/to enqueue/dequeue

Stack<int> stackAdd = new Stack<int>();
Stack<int> stackRemove = new Stack<int>();

public void Enqueue(int item)
{
	if (stackAdd.Count == 0) RevertStacks();	
	stackAdd.Push(item);
}

public int Dequeue()
{
	if (stackRemove.Count == 0) RevertStacks();	
	return stackRemove.Pop();
}

private void RevertStacks()
{
	if (stackAdd.Count > 0)
		while(stackAdd.Count > 0) stackRemove.Push(stackAdd.Pop());
	else if (stackRemove.Count > 0)
		while(stackRemove.Count > 0) stackAdd.Push(stackRemove.Pop());
}

- anvol April 05, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

we can do it using a single stack.
supported operations - top(), empty(), pop(), add()

for top() and pop() -
we recursively top or pop the elements until we get the last element. we will still be using two stacks.. one- the given stack and the other recursion stack.. but explicitly its just one stack!

wat say??

- anonymous November 07, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

recursion means you have used another stack

- Anonymous March 03, 2011 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

just one stack is fine.
insert - O(1)
dequeue - O(n)

- Anonymous November 27, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

And where would you store poped items? What you would do with them after poping? ;)

- anvol April 05, 2013 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

All the above solutions are for implementing FIFO queues. In the question, its not mentioned which queue has to be implemented.

The solution needs to take this into account.
In case of a LIFO queue, we just need push() and pop() {can be named add() and remove() }. Both will have time complexity of O(1). We also do not need two queues.

In case of FIFO queue, we need to make a design decision. This can be done either using an extra internal queue, or by using recursion. Adding an element to this stack based queue can be O(1).

void add(operation op)
{
	push(op);
}

For removing, we can use a recursive method

Operation remove()
{
	Operation op = pop();
	if(stack.count == 0)
	{
		return op;
	}
	else
	{
		Operation ret = remove();
		push(op);
		rerturn ret;
	}	
}

The above solution assumes that the Stack implementation supports both push() and pop() methods.

- Anonymous July 20, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

here, the recursive remove method will drill down the complete stack to get the last Operation (first operation that was added to the queue)

- Anonymous July 20, 2012 | Flag


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