Google Interview Question
1,072 Interview Reviews |
Back to all Google Interview Questions & Reviews
Interview questions and reviews posted anonymously by interview candidates
Interview Question for Senior Software Engineer at Google:
Create a stack of numbers where the maximum number is always known.
Helpful Question?
Yes |
No
Inappropriate?
Answers & Comments (6)
1 of 1 people found this helpful
insert(int p,stack s){
stack large;
int top = s.peek();
while(top>s){
large.push(s.pop());
top = s.peek();
}
s.push(p);
while(!large.empty()){
s.push(large.pop());
}
}
Helpful Answer?
Yes |
No
Inappropriate?
1 of 1 people found this helpful
while(top>p)
Helpful Answer?
Yes |
No
Inappropriate?
Helpful Answer?
Yes |
No
Inappropriate?
Helpful Answer?
Yes |
No
Inappropriate?
5 of 5 people found this helpful
The basic idea is that when a new number is being pushed onto the stack, you need to see if that number is greater than or equal to the current top of the maximums-stack. If it is, you push the number onto maximums-stack as well.
Also, when pop is called, you look to see if the number being popped is equal to the number on the top of the maximums-stack. If it it, pop that stack as well.
Helpful Answer?
Yes |
No
Inappropriate?
Members can
answer or comment on this question
–
Join Now (It's Free) or
Sign In
0 of 0 people found this helpful
by Interview Candidate: