Overstock.com Interview Question for Software Engineer / Developers


Country: United States
Interview Type: In-Person




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

Here is an implementation in JS

//3.5 Implement a Queue with Two Stacks
var stack = require("../../../lib/dataStructures/stack.js"); 

var stackQueue = function() {
    this.popStack = new stack();
    this.pushStack = new stack();
    this.length = 0;
};

stackQueue.prototype.moveToPop = function() {
    while(!this.pushStack.isEmpty()) {
        this.popStack.push(this.pushStack.pop());
    }
};

stackQueue.prototype.moveToPush = function() {
    while(!this.popStack.isEmpty()) {
        this.pushStack.push(this.popStack.pop);
    }
};

stackQueue.prototype.push = function(data) {
    this.moveToPush();

    this.pushStack.push(data);

    this.length++;
};

stackQueue.prototype.pop = function() {
    this.moveToPop();

    this.length--;

    return this.popStack.pop();
};

stackQueue.prototype.isEmpty = function() {
    return this.popStack.isEmpty() && this.pushStack.isEmpty();
};

stackQueue.prototype.peek = function() {
    this.moveToPop();

    return this.popStack.peek();
};

module.exports = stackQueue;

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

Queue can also be implemented using one user stack and one Function Call Stack.
Below is modified Method 2 where recursion (or Function Call Stack) is used to implement queue using only one user defined stack.

enQueue(x)
1) Push x to stack1.

deQueue:
1) If stack1 is empty then error.
2) If stack1 has only one element then return it.
3) Recursively pop everything from the stack1, store the popped item
in a variable res, push the res back to stack1 and return res
The step 3 makes sure that the last popped item is always returned and since the recursion stops when there is only one item in stack1 (step 2), we get the last element of stack1 in dequeue() and all other items are pushed back in step 3.

- Raj November 22, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I need step wise algorithm for implementing que with using single stack

- rahul September 20, 2016 | 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