Adobe Interview Question


Country: India




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

using System.Collections.Generic;

public class FastMinStack<T>
{
    private readonly Stack<T> stack = new Stack<T>();
    // Could pass this in to the constructor
    private readonly IComparer<T> comparer = Comparer<T>.Default;

    private T currentMin;

    public T Minimum
    {
        get { return currentMin; }
    }

    public void Push(T element)
    {
        if (stack.Count == 0 ||
            comparer.Compare(element, currentMin) <= 0)
        {
            stack.Push(currentMin);
            stack.Push(element);
            currentMin = element;
        }
        else
        {
            stack.Push(element);
        }
    }

    public T Pop()
    {
        T ret = stack.Pop();
        if (comparer.Compare(ret, currentMin) == 0)
        {
            currentMin = stack.Pop();
        }
        return ret;
    }
}

- Shock March 22, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I didn't understand, why the double push and pop? Why not just a single placeholder for the min value which will updated during Push and Pop by simply comparing with the current element?

- Tushar Bhasme April 03, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think idea is to keep minimum for all the pushed element so far in variable currentMin, but whenever lesser value comes to be pushed, currentMin is pushed first and then that new Value. Also currentMin is updated to new Value.

- Manohar Singh July 14, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Push into stack in sorted order.
As you can modify PUSH and POP.
implement both in sorted fashion.
the top of stack will have minimum element.
so every pop will give you the minimum element.

- sjain March 22, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
2
of 2 votes

No. You still need to maintain the integrity of the stack. i.e the pop operation should return the latest pushed element.

- 2Careers1Cup March 22, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

It can be done using two stacks. One for the element and other for the min.

Else you can modify the structure.

struct node
{
	int element;
	int min;
}

If even that can't be done. You can always push the minimum element behind the element.

class stack
{
	int* array;
	int size;
	int top;
	int min;
  
	stack(int s) { size = s; array = new int[2*s]; top = -1; min = INT_MAX; }
	~stack() { delete [] array };
	int pop();
	void push(int value);
	int get_min();
};

int stack::pop()
{
	if(top == -1)
		return INT_MIN;
	else
	{
		int t = array[top - 1];
		top = top - 2;
	}
}

int stack::get_min()
{
	if(top == -1)
		return INT_MIN;
	else
		return array[top];
}

void stack::push(int value)
{
	if(min > value)
		min = value;

	array[++top] = value;
	array[++top] = min;
}

- aasshishh March 22, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

While pushing elements, take a min pointer which always point to the minimum element. Every time you push, compare the coming element with the min element and change the min pointer accordingly. While retrieving the min element, push till you pop the min element.

- alex March 22, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

It is okay. But is there any way that we can do it logarithmic time rather linear time?

- ajit March 23, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

What do you mean?

- Tushar Bhasme April 03, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

This can be done using two stack. One, which is the actual stack and another stack which holds the minimum value seen till now at the top.

class MinStack {
	private Stack<Integer> theStack;
	private Stack<Integer> minStack;

	public MinStack() {
		this.theStack = new Stack<Integer>();
		this.minStack = new Stack<Integer>();
	}

	public void push(int value) {
		if(value <= this.min()) {
			this.minStack.push(value);
		}
		this.theStack.push(value);
	}

	public int pop() {
		int value = theStack.pop();
		if(value == this.min()) {
			value = this.minStack.pop();
		}
		return value;
	}

	public int min() {
		if(this.minStack.size() == 0) {
			return Integer.MAX_VALUE;
		}
		return this.minStack.peek();
	}
}

- Anonymous March 22, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

use another stack to keep track of minimum
stack 1 : stack 2:
push 2 push 2

stack 1: stack 2:
push 4 dont do anything

stack 1: stack 2:
push 1 push 1

Stacks contain the following:
stack 1: 1 4 2
stack 2: 1 2

When we pop from S1 check if it is equal to top of stack 2, if so pop from both stacks

- amber March 23, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void minStack(Struct Stack *s,int *min)
{
if(!isStackEmpty(s))
{
x=pop(s);
if(x<*min) *min = x;
minStack(s,min);
push(s,x);
}
}

- Shakti April 02, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

What's the value of min that you are passing while calling this function?

- Priyanka April 29, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

use 2 stacks .
2nd stack to keep track of current minimum.
time complexity O(1)

- amber April 29, 2013 | 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