VMWare Inc Interview Question
Software Engineer / Developersclass Node
{
public int value;
public Node nextNode;
public Node(int _value)
{
value = _value;
}
}
class stack
{
Node topNode; Node maxNode;
public int count = 0;
public stack(int _value)
{
topNode = new Node(_value);
maxNode = topNode;
++count;
}
public void push(int _value)
{
Node newNode = new Node(_value);
if(!(maxNode.value < _value))
{
maxNode = topNode;
newNode.nextNode = topNode;
topNode = newNode;
}
else
{
newNode.nextNode = topNode;
topNode = newNode;
}
++count;
}
public Node maxNode()
{
return maxNode;
}
public int pop()
{
int retval = topNode.value;
topNode = topNode.nextNode;
--count;
return retval;
}
public void sort()
{
Node currentNode = topNode;
while (currentNode != null)
{
Node runnerNode = currentNode.nextNode;
while (runnerNode != null)
{
if (currentNode.value > runnerNode.value)
{
int temp = currentNode.value;
currentNode.value = runnerNode.value;
runnerNode.value = temp;
}
runnerNode = runnerNode.nextNode;
}
currentNode = currentNode.nextNode;
}
}
public void print()
{
Console.WriteLine("Values of the node are");
Node currentNode = topNode;
while (currentNode != null)
{
Console.WriteLine(currentNode.value);
currentNode = currentNode.nextNode;
}
}
}
For the max()with 0(1), we need to maintain one more linked list that holds the nodes of maximum values and pop() it whenever requested.So the stack implementation requires 2 linked lists
you do not require second linked list. It can be done by keeping track of the max element in the top of the stack and manipulate it at each push.
while pushing only we need check for max....
s and top are pointers of type struct abc (.....struct abc *s)
//structure defn ......
/*
struct abc
{
int info;
struct abc * next;
};
struct abc *top=NULL;
struct abc *s; */
//code for checking max number
if(s->info > top->info)
max=(s->info)
else
max=(top->info)
same can be applied for finding minimum also......
while pushing only we need check for max....
s and top are pointers of type struct abc (.....struct abc *s)
//structure defn ......
/*
struct abc
{
int info;
struct abc * next;
};
struct abc *top=NULL;
struct abc *s; */
//code for checking max number
if(s->info > top->info)
max=(s->info)
else
max=(top->info)
same can be applied for finding minimum also......
The max value should be up to date after each pop as well... Say the maximum element was popped out, then you need to update the max value with the new maximum - how do you do that in your implementation ??
I'd say keep another field in each node saying max which is stored with the max value at the time it is being pushed onto the stack.... And always return the top's max value when max() function is called....
@Ash
then you need to update the max value with the new maximum
For this requirement, i guess we can make use feature of doubly linklist to maintain the nodes in sorted order.
node
{
int data;
node *next; \\to be used for normal push and pop;
node *nextmax, *prevmax; \\to have a list in sorted order
}
When you say return the node, what exactly does it mean ?Does it mean the address of the node ?
Is it necessary that we have to maintain the max element at the top of the stack. This is a linked list, so we can probably just store the address of the node containing the maximum values in a pointer.
Does that satisfy the requirement ?
- Girish October 09, 2010