Yahoo Interview Question for Software Engineer / Developers






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

take two stacks.in one stack put the value a/c to requirement,consecuitevily maintain the current min value in 2nd stack.
if u do the pop operation then compare it with 2nd stack top value if equal then also pop the top of 2nd stack .

- ujjwal akash September 17, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

organize the stack in such a way that when you are inserting a value , pop the stack , compare , push the greater one , then push the smaller one.

now , the smallest one will always be a push away.

- whattay!! September 06, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

the stack will no more be a stack

- anonymous September 10, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

pop the elements and save them in a BST

- lucy September 16, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

i took back what i said
declare another stack and keep a variable that stores the current min value

- lucy September 16, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

how all these solution are O(1)? you have to at least pop n elements from the stack, so it becoms order n isn't it?

- hell_down_under September 24, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

node with two address part one for next node and one for contain address of next min which min to that node value .

- rajnesh October 04, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This one looks promising. Each node maintains a pointer to the min value. Each time you push, you compare with the min value the top points to and set the appropriate pointer.

Below is an attempt.

/* No error checking done for simplicity */
 
  
struct MinStack {
    MinStack *next;
    MinStack *min;
    int value;
}
 
MinStack *Push(MinStack * stack, int value) {
     MinStack *top = (MinStack *)malloc(sizeof(MinStack));
     top->value = value;
     top->next = stack;
     if (stack->min->value <= value) {
        top->min = stack->min
     }else {
        top->min = top;
     }
}

MinStack *Pop(MinStack * stack, int *poppedValue) {
    *poppedValue = stack->value;
    MinStack *top = stack->next;
    free(stack);
    return top;
}

int Min(MinStack *stack) {
    return stack->min->value;
}

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

Good solution. Everyone should refer to this.

- Avinash October 05, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

What if minimum gets poped? then how will you point to the next min element?

- abhi1301 May 05, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Main two stack, with one stack having current min value

- Anonymous October 19, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

If it's any regular stack->impossible. If it's "my" stack, just make sure the min element is always on top.

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

Just keep a min variable .. which always checks before pushing in an element and return that element.

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

How do you handle pops?

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

If the question meant to say O(n) this is how I would do it:

assuming that we could check if stack is empty
pseudo code without any error checking

find_min(stack)
    n = stack.pop()
    m = stack.pop()
    s = smaller of n and m
    if stack is empty
        return s
    else
        stack.push(s)
        find_min(stack)

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

I guess you could check for trivial case where stack has only 1 item in it. Then just return that item, for that is the min.

- highwind May 26, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 2 vote

T's solution would work. Another approach if we were to avoid pointers is to store min value along with the element value in each stack element.

- Murali Mohan October 05, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

class Stack{
int top;
int[] stack;
MinimumStack miniStack;
// constructor for stack
Stack(int capacity){
top=0;
stack=new int[capacity];
miniStack = new MinimumStack(capacity);
}
boolean isFull(){
return(top==stack.length);
}
public void push(int val){
if(!isFull()){
stack[top++]=val;
miniStack.push(val);
}else{
System.out.println("The stack is full");
}
}

public Object pop(){
if(top!=0){
miniStack.pop(stack[top-1]);
return stack[top--];
}else{
return null;
}
}

public void displayStack(){
for(int i=0;i<top;i++){
System.out.println(stack[i]);
}
}

public void displayMinimum(){
System.out.println("The current minimum is"+miniStack.getMinimum());
}

}
class MinimumStack{
int topMin;
int[] Ministack;
public MinimumStack(int capacity) {
// TODO Auto-generated constructor stub
topMin=0;
Ministack=new int[capacity];
}
public int getMinimum() {
// TODO Auto-generated method stub
if(topMin!=0){
return Ministack[topMin-1];
}
return (Integer)null;
}
boolean isMinStackFull(){
return(topMin==Ministack.length);
}

public void push(int val){
if(!isMinStackFull()){
if(topMin!=0){
if(val<Ministack[topMin-1])
Ministack[topMin++]=val;
}
else{
if(topMin==0)
Ministack[topMin++]=val;
}

}else{
System.out.println("The stack is full");
}
}
public void pop(int val){
if(Ministack[topMin]==val){
int Minval=Ministack[topMin--];
}
}


}

- careerCupguy10 February 08, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

assholes..u guys take salary from company , do not work and then look for other jobs by doing all these bull shits...its all non sense..assholess

- amd December 19, 2014 | 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