Yahoo Interview Question


Country: India




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

stack 1		stack 2
	
push(5)		push(5)
push(2)		push(2)	(in stack2 if new data is less than top of stack then push new one)
push(7)		push(2)
push(20)	push(2)
push(9)		push(2)
push(1)		push(1)

now if you pop an element from stack 1, pop from second stack which is having min value till that point

stack 1		stack 2
pop()=>1	pop()=>1	1 is min so far
pop()=>9	pop()=>2	2 is min so far
pop()=>20	pop()=>2

- algos August 18, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

i could not understand the logic, please explain little bit..

- zeroByzero August 18, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Explain Logic please...

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

maintain 2 stacks
let the elements be 5,6,11,25,34,12,2,3
stack1 stack2
push 5
since 6>5 pop
elements from stack1 to
stack2 until top of stack>curr element.after finding the right position for current element empty stack2 to stack1

- deivanai September 01, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

we can optimise by using a condition like ...
whenever there is a number going to be pushed check the element on top of stack 2 if only its smaller than that ...push it
else dont push ...

- sreeram September 30, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

no need to push so many 2 in stack2, if it less than 2, push it, otherwise, do nothing to stack2

- shiqing881215 October 01, 2012 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

IIn that case,
if we push 2,1,1
then stack 2 would have 2,1
and then,
if we pop 1
then stack2 couldn't know that has to pop or not.

- hytgbn February 01, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

Maintain 2 stacks : Normal and minimumNoTracker.
Push: Everytime you push element in Normal stack, check the topmost element in MinimumNoTracker stack and if topmost element is greater than current, push the new element on MinimumNoTracker as well.
Pop: Everytime you pop element from Normal stack, check if it is same as top element of MinimumNoTracker, if yes pop out that number.

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

ya... I was also thinking the same

- Vipul August 19, 2012 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

Can't we use a sort on initial input if we have them stored. then push them in decreasing order ?

if we already have the problem where we are randomly generating number than putting them in stack, we need to use atleast 3 stacks, and this problem will pretty much looks like Towers of Hanoi problem, and we can use a recursive approach to solve it.

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

Basically you to create a variable in you stack impl class. Every time to push and entry it should compare to this variable if it is min then store that min number. Also you add getMin an isMin API to get and compare again this number....

- Prince shah August 17, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Based on what you're saying you only keep track of a single min value, and only update that when you push.

What if you pop twice in a row?

- Anon August 17, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 2 vote

why cant we implement min-heap here:
For insertion: insert at the top and min-heapify
For popping: take the top element and min-heapify

- cobra August 18, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

the question asked to implement the operations Push, Pop and GetMin in O(1) time...
you can get GetMin in O(1) time but it takes O(log n) time for push and pop as min-heapify requires O(log n) time to rearrange the heap to maintain the heap property.

- algos August 19, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

If insertion and popping is O(1), then insert n numbers, and pop the same n numbers. By the claim both operations would be O(n). Since popping gives the minimum number, we get a sorted list. Great, sorting in linear time.

- Anonymous August 18, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Idea is to write a new function pushbubble() using push(), where an element is inserted into its sorted position (situation similar to insertion sort). In that case, the top element will always be the minimum element.
Full working code here : awesomecoder.blogspot.com/2012/08/push-elements-into-stack-pop-should.html

void stack::pushbubble(int elem){
 int temp;
 if(!this->isempty()){
  temp = this->pop();
  if(temp > elem){
   this->push(temp);
   this->push(elem);
  } else {
   this->pushbubble(elem);
   this->push(temp);
  }
 } else {
  this->push(elem);
 }
}

- rkt August 18, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Like others have explained, maintain two stacks; one with the value and one with the min value in the other stack.

template <typename T>
class MinStack {
private:
  std::stack<T> stack;
  std::stack<T> min;
public:
  MinStack() {}
  void push(T elem) {
    stack.push(elem);
    min.push(std::min(elem, min.top());
  }
  std::pair<T, T> popValAndMin() {
    T val = stack.top();
    T minVal = min.top();
    stack.pop();
    min.pop();
    return std::make_pair(val, minVal);
  }
};

- Martin August 20, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Tag

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

maintain 2 stacks

- deivanai September 01, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

i wont write any code yet, but the list should be sorted first. And we know that stack is last one in and first out, so numbers are put into stack in descending order. Popping the smallest element will take o(1) since the smallest will be the last one in.

- tenon2000 September 14, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

If this was possible, you could sort in linear time.

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

agree, for F heap, insertion is O(1) but popping is O(logn)

- hwer.han August 17, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Wrong. There's no way you could use this to sort in linear time. That's why it's possible, and why there's a solution on this page that does it.

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

Maintain each stack node with data and min value (beneath itself).

- Abhay August 17, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
-2
of 2 vote

Basically, you can build a priority queue using Fibonacci Heap. Operation on F Heap is O(1).

- hwer.han August 17, 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