Amazon Interview Question for Software Engineer / Developers


Country: United States
Interview Type: Phone Interview




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

Q1 and Q2

For O(1) push() and O(N) pop():

1. Enqueue into Q1.
2. If pop, check size of Q1, if size > 1, dequeue into Q2 until size == 1
3. Dequeue from Q1, return result
4. Switch back and forth

For O(N) push() and O(1) pop():

1. push() -> Enqueue into Q1
2. push() -> Enqueue into Q2, and Dequeue Q1 into Q2
3. repeat back and forth
4. pop() -> Dequeue from the Q that has element

- soconfusedgrad October 31, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 2 vote

public class StackWithQueue
    { 
        private Queue<char> Q1= new Queue<char>();
        private Queue<char> Q2 = new Queue<char>();

        public void Push(char ch)
        {
            if (Q1.Count == 0)
            {
                Q1.Enqueue(ch);
                MoveQItems(Q2,Q1); // Move items from Q2 to Q1
            }
            else
            {
                Q2.Enqueue(ch);
                MoveQItems(Q1, Q2);
            }
        }

        public char Pop()
        {
            if (Q1.Count != 0)
            {
                return Q1.Dequeue();
            }
            else if (Q2.Count != 0)
            {
                return Q2.Dequeue();
            }
            return ' '; 
        }

        private void MoveQItems(Queue<char> a, Queue<char> b)
        {
            while (a.Count != 0)
            {
                b.Enqueue(a.Dequeue());
            }
        }

}

- SHAD October 31, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

PushQueue, PopQueue.
1. starting, both empty.
2. push operation done by "PushQueue.push()"
3. pop operation done by

int t = 0;
while (t < (PushQueue.size() - 1)) {
    PopQueue.push(PushQueue.pop());
}
return PushQueue.pop();

4. swap PushQueue and PopQueue.

- peter tang October 31, 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