Yahoo Interview Question






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

Is that possible? unless you keep extra variable for stroing min value. Update this value when ever you insert and pop an element from stack.

- Erik March 25, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

"Space constraint has to be considered" does not necessarily mean you cannot use any extra space.

Also, in your solution, how will you handle the pops with just one min variable?

- T March 25, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Well, with this solution pops can take up to O(n) time to maintain min value. But min value of a stack and pushes will be O(1)

- Evgeny April 05, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

whenever we push an element on to stack , have an extra variable min with the struct element .. compare the element which you are pushing on to the stack with the element's min field on top of the stack . This will ensure that top element's min field does have the min value of the stack .. ( this is with out considering space complexity as a constraint) .

- Anonymous March 26, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

awesome!!!! way to go buddy

- clrs March 02, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Well, if that was the case, lets say we had 
0
5
3

If we pop 0, then, we have
5
3

So, now, your element to store the temp variable is invalid.

But, if we had another stack to store the least variable and a pointer to mapping the the corresponding element in the original stack, it's possible to keep track of the least element in the stack and pop it off the smallest stack when the actual element is popped.

So, 
insert 3

actual stack      smallest stack
------------      --------------
3                 3


insert 5:

actual stack      smallest stack
------------      --------------
5                 3 
3


insert 0:

actual stack      smallest stack
------------      --------------
0                 0
5                 3
3


pop 0:


actual stack      smallest stack
------------      --------------
5                 3
3                 

pop 5:


actual stack      smallest stack
------------      --------------
3                 3

- Balaji March 26, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

easy question, just have another atom which takes min number. it takes a time every atom pushed. but you can take min element with 0(1)

- aian March 26, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

AGAIN!

How will you update the atom during the pop operation?

- T March 26, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Answer posted by Balaji is good. Use two extra stacks for max and min. You can get max and min in O(1).

- Erik March 26, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

while pushing just keep the min elemen

- anonymous March 26, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

What about min heap?
a heap node can have 1) element, 2) ptr to keep min heap structure, and 3) ptr to keep track of stack data structure.

- freeaion April 01, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Min heap is not an option as question is about stack.

- Amit April 01, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Bala Ji Solution seems correct. but it violates space constraint requirement.

In case of below push we need stack of equivalent size.

9,8,7,6,5,4,3,1

Any other idea.

- amit April 01, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Amit, you are right. The problem is all about the stack.
What I want to say is modified min heap structure.
To be more concise, heap node might contain 1) element and 2) ptr to next stack top along with the current stack top.
As we know, min heap structure does not need to have any ptr when array is used.
it's because it is a complete binary tree.

I think Bala Ji solution is great. :)

- freeaion April 02, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

We can have stack with each entry having additional pointer. The stack functionality of push and pop will be as normal. But every time we push/pop, this additional pointer needs to be updated to point to next larger value

- Robin April 02, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I doubt if this question is correct as a given stack contains unsorted elements and we will have to see the elements atleast once to get the min/max so we cant avoid the O(n) time anyways.

- algooz June 24, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

How about we maintain an internal linked list for min element. Each time we push we create accordingly push it at the head/insert it at the appropriate place on the internal linked list. Thus min would be O(1) and when we pop, we compare it with the head of the linked list, if it is "the" element which is popped then we can delete the head from our internal linked list and make the second min element as our new top element.

- SNL September 14, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

SNL: the question is about stacks and you're not supposed to use liknedlist to get this problem done.

- Anonymous September 15, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

You can find the solution to this problem in Advanced Data structures book by peter brass ..i didnt get any online link , but you can search that book to find the solution its a real nice book i got pdf version in google itself .

- Anonymous September 15, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Balaji's solution is the way to go.

- Anonymous September 15, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

just keep a pointer to the smallest element...each time u insert compare it wid the value being inserted..if its smaller, shift your pointer else leave it

- bla October 29, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

That doesn't work bla. How would you update that pointer that stores the minimum value when the min gets popped.

Actually you are partially right.

The push will be O(1)
The pop will be O(n) at the worst case

- Sudipta October 30, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

The problem says to find the min in O(1) time. If taken literally, then we can maintain the extra variable as suggest before. Then whenever popped we adjust the min by some scanning. Then still we would be able to maintain the condition of O(1) complexity of finding the min. Not every time we are going to pop the min element. So amortized complexity of pop might be (and should be) less than O(n). So dont worry much about the pop.

- Anonymous December 26, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

make each element of the stack to store two values
stack value,min value at the time of its insertion
and we need one more int value to keep the current min.
while pushin we can update the current min
while poping (if we are poping the min value) we can get the previous min value by reading the top element's min.

- sudheer July 01, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Java Implementation of Minimum stack using LinkedList (not the class) Push, Pop and getMinimum() requires O(1).

class Node {
	Node next;
	Node nextMin;
	int value;

	public String toString() {
		return value + "";
	}
}

class MinStack {
	Node top;
	Node minNode;
	int size;

	public void push(int data) {
		Node node = new Node();
		node.value = data;
		if (top != null) {
			node.next = top;
			if (node.value < minNode.value) {
				node.nextMin = minNode;
				minNode = node;
			}
		} else {
			minNode = node;
		}
		top = node;
		size++;
	}

	public Node pop() {
		if (top == null) {
			return null;
		}
		Node tmp = top;
		if (minNode.value == tmp.value) {
			minNode = tmp.nextMin;
		}
		top = tmp.next;
		size--;
		return tmp;
	}

	public Node getMinNode() {
		return minNode;
	}
}

public class MinStackTest {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		MinStack minStack = new MinStack();
		minStack.push(10);
		minStack.push(2);
		minStack.push(12);
		minStack.push(3);
		minStack.push(0);
		minStack.push(1);
		int size = minStack.size;
		for (int i = 0; i < size; i++) {
			System.out.println("Before:" + minStack.getMinNode());
			System.out.println("Popped:" + minStack.pop());
			System.out.println("After:" + minStack.getMinNode());
		}
	}
}

- Singleton July 03, 2010 | 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