Microsoft unknown Interview Question for Software Engineer / Developers






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

<pre lang="java" line="1" title="CodeMonkey634" class="run-this">public class MinStack<T extends Comparable<T>>
{
private Node<T>[] table;
private int size;

public T pop() {
if (this.isEmpty()) {
return null;
}

return table[--size].item;
}

public T min() {
if (this.isEmpty()) {
return null;
}

return table[--size].min;
}

public void push(T item) {
if (size == table.length) {
this.resize();
}

if (this.isEmpty()) {
table[size++] = new Node(item, item);
} else {
Node<T> top = table[size - 1];
T min = top.min.compareTo(item) < 0 ?
top.min : item;

table[size++] = new Node(item, min);
}
}

public boolean isEmpty() {
return this.size == 0;
}

private void resize() {
int newCapacity = 2 * this.table.length;
if (newCapacit < 0)
newCapacity = Integer.MAX_VALUE;

Node<T> newTable = (Node<T>) new Node[newCapacity];
System.arraycopy(table, newTable, size);

this.table = newTable;
}

private static class Node<T>
{
public final T item;
public final T min;

public Node(T item, T min) {
this.item = item;
this.min = min;
}
}
} </pre>

- Ryan August 16, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

In the min() function, the last line should be

return table[size - 1].min;

- Ryan August 16, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

What happens if I pop the minimum?

- S August 16, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Depends on what the interviewer ask. Typically this question only ask for getMin() in O(1).
To pop min, I dont think it can be done in O(1).

- Ryan August 16, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Basically you just need another stack S for storing the minimums. S.top() gives you the current min. When inserting a new item, call S.push(min(S.top(), item)). When popping an item, you just call S.pop().

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

suppose your minimum stack is

10,9,8,7,6,5,4

and now you push 11

after s.push(min(s.top(),11))

you have

10,9,8,7,6,5,11,4

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

All 3 operations in O(1)? I dont think that's possible.

- DRY August 16, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

My bad. It IS possible.

- DRY August 16, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

It is impossible because the following: as we know, sort n numbers needs at least O(nlogn) time, if all three operations can be done in O(1) time, then we can sort in O(n) time, which is a contradiction.

- Anonymous August 20, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

oops! you gave a proof as well..

Its possible with extra space (extra stack in this case)

- Mahesh September 09, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

you can't achieve o(1) with stack
one of the tree functions must be o(n)

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

If we just keep a track of the new elements that are being pushed into the stack and comparing it with the existing min value , ( and hence updating as necessary ) , we can complete the task in O(1) time . And as for the push and pop things , they require O (1) in the first place , so no hassle there .

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

From what I got from the question
Push and pop in stack always take O(1) time but to find minimum in the stack it will take O(n) time in the worst case...
So I think its not possible to find min in O(1) time untill the stack is sorted in decreasing order...

- ibnipun10 August 17, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

This can be done by tweaking push(). While inssrting just check whether previous number is greater than the one which is being inserted.
This way Stack will be sorted and min is done in O(1) time.

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

Stack should always be LIFO. Tweaking is not an option.

- ysjaitawat September 16, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

He doesnt mean 'tweaking', he means to keep track of the min as we push. lifo would still be accomplished.

- Sat September 22, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

But you can not find and set the next minimum in O(1) when current minimum is popped. Thats where the problem arises.

- erm October 02, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

the real question is similar to wat asked to me...
perform push and min() in 0(1) time...wid pop,not more than 0(n) time

- Anonymous September 03, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

I do second Sayak.

Basically all_ele_stack shall push all elements into it. Whereas min_ele_stack shall only push only the "running minimum" elements.

Push
-----
all_ele_stack.push(newElement)
if (newElement < min_ele_stack.peep())
min_ele_stack.push(newElement)

Pop
---
data = all_ele_stack.pop();
if (data == min_ele_stack.peep())
min_ele_stack.pop()
return data;

Min
---
return min_ele_stack.peep();


So it solves the problem with extra space complexity of O(n).

It assumes no duplication of the input data.

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

Pradip

What if we insert numbers in decreasing order.
In this case the space complexity will be O(n) rite?

How can we here say that there is no duplication of data when we are storing min element. There is some duplication rite?

- ibnipun10 August 18, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

class LinkStack
{
static void Main(string[] args)
{
LinkStackX theStack = new LinkStackX(); //make stack

theStack.push(1, 20); //push items
theStack.push(2, 40);

theStack.displayStack(); //display stack (40, 20)

theStack.push(3, 60); //push items
theStack.push(4, 80);

theStack.displayStack(); //display (80, 60, 40, 20,)

theStack.pop(); //pop items (80, 60)
theStack.pop();

theStack.displayStack(); //display stack (40, 20)
}

class LinkStackX
{
private LinkedListX.LinkListX_ pList; //pointer to linked list

public LinkStackX() //constructor
{
pList = new LinkedListX.LinkListX_();
} //make a linked list

//--------------------------------------------------------------
public void push(int id, double j) //put item on top of stack
{
pList.insertFirst(id, j);
}

//--------------------------------------------------------------
public double pop() //take item from top of stack
{
return pList.removeFirst();
}

//--------------------------------------------------------------
public bool isEmpty() //true if stack is empty
{
return (pList.isEmpty());
}

//--------------------------------------------------------------
public void displayStack()
{
Console.WriteLine("Stack (top-->bottom): ");
pList.displayList();
}
}
}

- Meruzhan August 19, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

class LinkedListX
{
public class Link
{
public int iData; //data item
public double dData; //data item
public Link pNext; //ptr to next link in list

//-------------------------------------------------------------
public Link(int id, double dd)
{
iData = id;
dData = dd;
pNext = null;
}

//-------------------------------------------------------------
public void displayLink() //display ourself {22, 2.99}
{
Console.WriteLine("iData = " + dData);
}
//-------------------------------------------------------------
}

public class LinkListX_
{
private Link pFirst; //ptr to first link on list

public LinkListX_() //constructor
{
pFirst = null;
} //(no links on list yet)

//-------------------------------------------------------------
public bool isEmpty() //true if list is empty
{
return pFirst == null;
}

//-------------------------------------------------------------
//insert at start of list
public void insertFirst(int id, double dd)
{ //make new link
Link pNewLink = new Link(id, dd);
pNewLink.pNext = pFirst; //newLink-->old first
pFirst = pNewLink; //first-->newLink
}

//-------------------------------------------------------------

public Link getFirst() //return first link
{
return pFirst;
}

//-------------------------------------------------------------
public double removeFirst() //delete first link
{ //(assumes list not empty)
Link pTemp = pFirst; //save first
pFirst = pFirst.pNext; //unlink it: first-->old next
double key = pTemp.dData;
pTemp = null; //delete old first
return key;
}

//-------------------------------------------------------------
public Link find(int key) //find link with given key
{ //(assumes non-empty list)
Link pCurrent = pFirst; //start at ‘first’
while (pCurrent.pNext != null &&
pCurrent.iData != key) //while no match,
{
pCurrent = pCurrent.pNext; //go to next link
}
return pCurrent; //found it
}

//-------------------------------------------------------------
public bool remove(int key) //remove link with given key
{ //(assumes non-empty list)
Link pCurrent = pFirst; //search for link
Link pPrevious = pFirst;
while (pCurrent.pNext != null &&
pCurrent.iData != key)
{
pPrevious = pCurrent; //go to next link
pCurrent = pCurrent.pNext;
} //found it
if (pCurrent == pFirst) //if first link,
pFirst = pFirst.pNext; //change first
else //otherwise,
pPrevious.pNext = pCurrent.pNext; //bypass it
pCurrent = null; //delete link
return true; //successful removal
}

//-------------------------------------------------------------
public void displayList()
{
Console.WriteLine("List (first-->last): ");
Link pCurrent = pFirst; //start at beginning of list
while (pCurrent != null) //until end of list,
{
pCurrent.displayLink(); //print data
pCurrent = pCurrent.pNext; //move to next link
}
}
}
}

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

Easy enough. Use a stack subclass, with an inner stack just for tracking min values.

When pushing, peek into the min stack and compare. If it's less than the min, push the min stack.
When popping, compare to the top of the min stack, and pop the min stack if it's the same object.

- Inverse.Falcon August 21, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

little modification. while pushing if the new value is LESS THAN EQUAL TO peek value then push in the min stack.

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

Ah, yes. Popping goes out of sync otherwise. Good catch!

- Inverse.Falcon August 21, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think you should also track the pushing into some sort of "0;1" array: 0 - push was in the min stack; 1 - push was on the other stack.
So, if you have something like: 00111101011000 => Pop() = pop from min 3 times, from other 2 times, 1 time from min stack, etc.

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

I don't get it. What if the new number /isn't/ the new min? Now you have to find /how/ 'min' it is, ie you have to do a lg(n) binary search of the min stack to find where it should go.

If instead you don't do anything with the min stack, but instead just throw the 'not-min' value away, well, a few popped items later, you don't have a true stack of mins anymore; maybe 3 down is where that 'not-min' should have gone, and instead you've got the 4th down, which is > the 3rd min would have been.

However, I can't think of an O(1) solution.

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

Since you're working with a stack, there's no problem. The only way for an item to get removed from the stack is to pop it from the top.

We don't need to know how 'min' a value is if it isn't the minimum. Why? Because it's a stack! You'll need to pop that 'not min' value BEFORE you can pop the current min value.

Simple example: Push 4, 5

1. 4 is pushed. Since there is no min, 4 is pushed on the min stack.

2. 5 is pushed. Since 5 > 4 (min), we don't push 5 on the min stack.

5 CANNOT be the min until 4 is popped. But 4 can't be popped until 5 is popped first. And that would just get us an empty stack.

- Inverse.Falcon August 21, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Assume the following are pushed with the given order:

3, 1, 5

Then the main stack shall be: (top) 5 1 3 (bottom)

and the min stack shall be: (top) 1 3 (bottom)

At that moment, what happens if I call min()?
1 is popped out of the min stack but what happens to the "1" in the main stack?

The only way to handle this in my opinion is implementing the main stack as a linked list, holding a pointer for each element in the min stack pointing to its location on the main stack and removing that also from the main stack when min() is called.

Any ideas?

- erm October 02, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

min() isn't popping any values. Think of it as a peek() of the min value in the stack. If the requirements were different you would need a different approach, but min() doesn't alter the stack, so there's no problem.

- Inverse.Falcon October 02, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Thanks Inverse.Falcon.

For others as slow as I, indeed you only push the mins onto the min stack as they are smaller than the top of the min stack. Say you push (total) 1, 2, 3. 1 is at the bottom of the main, internal stack, then 2, top 3. 1 is the /only/ contents of the min stack. Popping the 1 would require you first to pop 3, then 2, so 1 is throughout, correctly, the min (until you pop it). You don't need to store all numbers on the min stack in min order; the top min on the min stack serves as the min for all numbers above where it (also) is on the main, internal stack.

- JeffD August 22, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

The idea of using binary representation to keep track of whether the element is included in the main stack or min stack is good. But it will limit this algorithm to 32 items on a 32 bit machine and 64 items on a 64 bit machine. So its better we use the main stack to push and pop all the elements and in addition we will have min stack to update the min element.

- genthu August 22, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

one of the possible solution can be-
1.suppose we have stack(can be implemented through class),and top(indicates top position of the stack and min which indicates minimum element in stack
2.pop(),push() can be implemented O(1) as usually(since push and pop takes place on top) and for min
whenever we are inserting an element in to stack we update min as
if(element<min)
min=element
else
min=min

- ramukumar September 08, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Agree. simple and best.

- cirus September 28, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

That's not going to work.

You're only thinking about pushing right now, and updating the min that way. Your approach works well here.

But now consider the implications of popping the current min. Since you're not tracking the min values, you would need to search the stack for the new min, and that's not O(1). Simply keeping a single min reference isn't going to work.

- Inverse.Falcon October 02, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

I dont know what the fuss is about ...
1. if arr[i]<min
min=arr[i]
2. push
3 pop

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

Inverse.Falcon, nice approach.
I would like to add one more thing to take care of multiple instance of same value.
As min is popped u will pop it from minStack as well, but if there is one more element down in the stack then min will return wrong value.
So if new element is same as min push min again to both the stacks.

@ramukumar as soon as min is popped out "min" will be out of sync.

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

@ysjaitawat
actually min indicates min element in stack which is updated accordingly whenever we insert element in to stack whenevr we calll minimun() just return min

- ramu kumar September 16, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Yes, and as soon as by pop() the minimum number is pooped from stack, how would you calculate minimum in o(1).

- ysjaitawat September 20, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

1. Create 2 stacks named RegularStack and MinStack.
2. RegularStack: Implement normal push(), pop() and top() functions.
3 MinStack: Maintain the minimum element in RegularStack as its top.

1) Push:
While pushing an element into RegularStack, Compare this element with the top element in MinStack.
If it is smaller, then push that element into MinStack at the same time.
Else do nothing.

2) Pop:
While popping an element out from RegularStack, Compare this element with the top element in MinStack.
If they are equal, we pop out the top element from MinStack at the same time. Otherwise, we do nothing.

3. Get:
Peek element from MinStack and Return.

class StackOperations
{
    Stack<int> RegularStack = new Stack<int>();
    Stack<int> MinStack = new Stack<int>();
    StringBuilder StringBuilderObj = new StringBuilder(); 

    public void PushPopGetMinWithConstantTime()
    {
        Push(3);
        Push(4);
        Push(5);
        Push(1);
        Push(2);
        StringBuilderObj.Append("\nElements Pushed into Stack:\n 3,   4,  5,  1,  2");
            
        int minElement = GetMinElement();
        StringBuilderObj.Append("\n\nMin Element in Stack:\t" + minElement.ToString());
        
        int poppedElement = Pop();
        poppedElement = Pop();
        StringBuilderObj.Append("\n\nElements 1,  2 popped out from Stack.\t");

        StringBuilderObj.Append("\n\nRemaining Elements in Stack:\n 3,   4,  5");

        minElement = GetMinElement();
        StringBuilderObj.Append("\n\nMin Element in Stack:\t" + minElement.ToString());

        MessageBox.Show(StringBuilderObj.ToString());
    }

    public void Push(int elementToPush)
    {
        //Push element to RegularStack
        RegularStack.Push(elementToPush);

        //If the elementToPush is less than the currentMinElement in MinStack then insert elementToPush to MinStack
        //Else do nothing.
        int? currentMinElement = null;

        if (MinStack.Count > 0)
        {
            currentMinElement = MinStack.Peek();
        }

        if (currentMinElement == null || elementToPush < currentMinElement)
        {
            MinStack.Push(elementToPush);
        }
    }
    public int Pop()
    {
        if (RegularStack.Count == 0)
        {
            throw new Exception("No elements in stack to pop");
        }

        //Pop element from RegularStack.
        int elementPopped = RegularStack.Pop();

        //Get element from MinStack and check if it is same as elementPopped, then pop out it from the MinStack as well.
        int currentMinElement = MinStack.Peek();
        if (elementPopped == currentMinElement)
        {
            MinStack.Pop();
        }

        return elementPopped;

    }
    public int GetMinElement()
    {
        if (MinStack.Count == 0)
        {
            throw new Exception("No elements in stack to pop");
        }

        //Get element from MinStack and return.
        int currentMinElement = MinStack.Peek();
        return currentMinElement;
    }
}

- Srigopal Chitrapu January 29, 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