Amazon Interview Question for SDE1s


Country: United States




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

Implement it using doubly linklist... and use an int variable to store the address of current number of elements in stack and a pointer variable to store the address of middle element.

- Hector January 09, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

import java.util.ArrayList;

public class Stack 
{
	private ArrayList<Integer> stack;
	
	public Stack()
	{
		stack = new ArrayList<Integer>();
	}
	
	public int push (int value)
	{
		stack.add(value);
		return stack.size() - 1;
	}
	
	public int pop ()
	{
		int value = stack.get(stack.size() - 1);
		stack.remove(stack.size() - 1);
		return value;
	}
	
	public int getMiddle ()
	{
		return stack.get(stack.size()/2);
	}
	
	public int deleteMiddle ()
	{
		int value = stack.get(stack.size()/2);
		stack.remove(stack.size()/2);
		return value;
	}
}

For the sake of brevity, the middle element is the 3 when there are 6 elements in the ArrayList. i.e [0,1,2,3,4,5]

- buckeyekarun January 10, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

1)public boolean remove(Object o), remove takes object as input, your array seems to be containing 0,1,2,3,4,5.... only try 2,3,6,2,5

2) complexcity of remove is O(N) not constant

- Ravi January 16, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

You can have a doubly linked list as stack having pointers to start, end, middle and integer top, mid

update middle and mid in case top/2 != mid whenever push or pop takes place.

- Ravi January 16, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Use flag to check if the length of the stack is even or odd and then accordingly update the mid element

class Stack {

    int top;
    StackNode middle;
    StackNode last;
    boolean flag;

    Stack() {

        top = 0;
        middle = null;
        last = null;
        flag = false;//odd elements -true
    }

    void push(int data) {
        StackNode newNode = new StackNode();
        newNode.data = data;
        if (last == null) {

            last = newNode;
            middle = newNode;
            flag = true;
        } else {
            StackNode temp = last;
            last = newNode;
            temp.next = newNode;
            newNode.previous = temp;
            flag = !flag;

            if (flag) {

                middle = middle.next;
            }

        }

    }


    void print() {

        StackNode temp = last;

        while (temp != null) {

            System.out.println(temp.data);
            temp = temp.previous;
        }
    }

    int getMiddle() {

        return middle.data;
    }

    void deletMiddle() {

        StackNode temp=middle;
        middle.previous.next=middle.next;
        middle.next.previous=middle.previous;
        flag=!flag;
        if(!flag){
            middle=temp.previous;
        }
    }
}

class StackNode {

    int data;
    StackNode next;
    StackNode previous;

    StackNode() {
        previous = null;
        next = null;
    }


}

- geekgirl January 22, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

AFAIK, ArrayList(java) or vector(c++) can be used to implement the stack(push and pop on the last element). Indexing the middle element of either would be a constant time operation.

- Dheeraj Shetty January 09, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Sorry, missed deleteMiddle(). Please disregard my answer

- Dheeraj Shetty January 09, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

you can use erase to deletemiddle

- Anonymous January 15, 2014 | 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