unknown Interview Question for Software Developers


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

- rrreeeyyy May 09, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- pavel.em May 19, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

please forget my previous comment

- pavel.em May 19, 2015 | Flag
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() {
		return top.high.value;
 	}
}

- Gil May 09, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Whaw will be the space complexity of this one?

- A3Clef May 09, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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?

- SK May 10, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

max heap

- someguy May 08, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

max heap

- someguy May 08, 2015 | Flag Reply
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)

- seancheren May 09, 2015 | Flag Reply
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)

- SK May 09, 2015 | Flag Reply
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;
}

- justinm May 18, 2015 | 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