Interview Question


Country: India




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

The solution that I am about to suggest may not be very performant nor practical (in multi-threaded application, but let me know if it makes sense.

1. If the Queue does not keep a count of its elements, add a count property.
2. For each Pop operation of the stack
- Perform the following (Count -1) times
- Dequeue() an element and then Enqueue() the same element
3. Dequeue the element and return it (It should the top of the stack).

Adding base conditions and error & boundary checking, this Queue should work like a stack.

- NewCoderInTown April 19, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This works and is O(n) amortized.

Let us not talk about practicality in questions like "make stack using only one queue", which probably will never arise in real life, apart from testing problem solving abilities of a candidate in an interview.

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

Sorry that was O(1) amortized.

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

Of course you need to keep track of the count when you have to do the cycle again (which is missing from above solution).

Basically you need to maintain the number of elements which can be popped by Deque. If that count goes to zero, you do the cycle.

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

Here is some (psuedo)code trying to use the above answers

class <T> Stack {
    public push(T elem) {
        _stack.Enqueue(elem);
    }
    
    public T pop() {
        if (_stack.IsEmpty()) throw StackEmptyException;
    
        if (numPushable == 0) {
            int numToCycle = _stack.Count;
            while (numToCycle > 0) {
                T elem = _stack.Dequeu();
                _stack.Enqueue(T);
                _numPushable++;
                numToCycle--;   
            }
        }
        _numPushable--;
        return _stack.Dequeu();
    }
    private Queue<T> _stack;
    private int _numPushable = 0;
}

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

This an incorrect solution.

push push push pop push push pop

will give wrong answer.

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

Getting O(1) amortized is an open problem, even with two queues.

One solution is, on a pop, to reverse the Queue (by deque/enque multiple times), get the element which needs to be popped, then restore the queue back to the original queue state.

So push is O(1) but pop is O(n).

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

Yes this works I think.

Code:

class <type T> Stack {
    public void push(T elem) {
        _stack.enque(elem);
    }
    
    public T pop() {
        _stack.reverse();
        T elem  = _stack.deque();
        _stack.reverse();
    }
    private Queue<T> _stack;
}

What if we wanted a fast pop, but push could be slower?

- Anonymous April 24, 2012 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

[link]lmgtfy.com/?q=careercup.com%3Aimplement+a+stack+with+single+queue[/link]

Try it before you post a question here

- richa.shrma.iitd April 19, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

You don't get the point of this site, Richa.

What good does it do a candidate to post a question here, if they have already been through it in an interview and have failed to answer it?

The point of posting here is to let _other_ people know what kind of questions are being asked.

This site is just a repository of questions. People post the problems they were asked, not necessarily the problems they want the solution to, irrespective of whether a web-search gives the solution.

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

Yeah, so a -1 for that inane lmgtfy link.

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

No Anon this website is very disorganized already. I propose a stack overflow kind of implementation.

- stupid Anon April 21, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

No Anon this website is very disorganized already. I propose a stack overflow kind of implementation.

- stupid Anon April 21, 2012 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

node *p,*r,*q;
p=malloc(sizeof(node));
p->x=value;
p->next=null;
r=p;
while(1)
{
q=malloc(sizeof(node));
q->x=value;
q->next=p;
p=q;
}
now it retrieve list with p it follows FIFO form

- istiyak916 April 19, 2012 | 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