Facebook Interview Question: use stack to pop out the max ... | Glassdoor

Interview Question

Software Development Engineer Interview(Student Candidate) Madison, WI

use stack to pop out the max num under O(1)


Interview Answer

2 Answers


my way is too complex...
Good answer would be using Class or 2 stacks, and keep record the current max

Interview Candidate on Feb 24, 2012

Using c++ and 2stack

std::stack S;

void addToStack(int value)
    std::stack T;

    while(!S.empty() && (S.top() > value)) {
        int V = S.top();


int getMaxValue()
    int value = S.top();
    return value;

int main()

    int max = getMaxValue();
    std::cout << max << std::endl;

Anonymous on Nov 12, 2012

Add Answers or Comments

To comment on this, Sign In or Sign Up.