## unknown Interview Question for Software Developers

• 0

Country: United States

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

max stack (consist of:
stack1: regular stack
stack2: max stack
)

push(x){
stack1.push(x)
if (stack2.top<=x) stack2.push(x)
}

pop(){
stack1.pop
if (stack2.top==x) stack2.pop
}
all operations are O(1).

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

that's not working otherwise you would create a max heap with insert operation O(1)

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

please forget my previous comment

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

Stack with an internal node that maintains a link (lets call it 'High') to the current highest value node in the stack.
Each time we push in a new node, we compare the new value to the 'High' in the top element. and we choose the higher value for the new node 'High'.

With this, we maintain the 'High' value in O(1) in push and in pop.

----
----

Push O(1)
Pop O(1)
PeekHigh O(1)

----
----

``````//schematic code, not full code

public class Stack<T> {
private class Node {
T value;
Node next;
Node high;
}

private Node top;

public void push(T value) {
top = new Node(value,top);
if (top.next != null && top.value.compareTo(top.next.value) < 0) {
top.high = top.next;
} else {
top.high = top;
}

}

public T peekHigh() {
}
}``````

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

Whaw will be the space complexity of this one?

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

What happens when you pop the highest element?

Say I add, 2,3,4,5 in that order and i remove 5. How will I know that the new max is 4?

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

max heap

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

max heap

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

A Stack with a collection of N items.

Space: O(n)
Push: Time: O(1)
Pop: Time: O(1)
Peek Time: O(n)

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

Stack + Max heap.

Push - O(logn)
Pop- O(logn)
Peek - O(1)

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

Similar to Gils idea but each item on the stack records the max value at time it was added.
Push, Pop, Peek all O(1)
Space complexity is 2n

``````function StackEx() {
this.items = [];
}

StackEx.prototype.push = function (val) {

if (this.items.length === 0) {
this.items.push(new StackExItem(val, val))
} else {
var maxVal = Math.max(this.items[this.count() - 1].maxValue, val);
this.items.push(new StackExItem(val, maxVal));
}
}
StackEx.prototype.pop = function () {
return this.items.pop().value;
}
StackEx.prototype.peekHighest = function () {
return this.items[this.items.length - 1].maxValue;
}
StackEx.prototype.count = function () {
return this.items.length;
}

function StackExItem(val, maxValue) {
this.value = val;
this.maxValue = maxValue;
}``````

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.

### 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.