Question 71
HardPushes are 1, 2, 3, 4, 5 in order with pops allowed in between. Which pop sequence is possible?
- A.
Popular companies
Prepare company by company
Hiring pattern, syllabus, past questions and mocks for each company.
All companiesPractice by subject
Data Structures and Algorithms
Pushes are 1, 2, 3, 4, 5 in order with pops allowed in between. Which pop sequence is possible?
3, 2, 5, 4, 1
1, 5, 2, 4, 3
5, 1, 2, 3, 4
3, 1, 2, 5, 4
Sign up free to see the answer and a step-by-step explanation.
How many different pop orders are possible when 4 distinct items are pushed in a fixed order?
10
24
16
14
Sign up free to see the answer and a step-by-step explanation.
What is the time complexity of the 'largest rectangle in a histogram' solution using a stack?
O(n²)
O(1)
O(n log n)
O(n)
Sign up free to see the answer and a step-by-step explanation.
How does the stack-based algorithm for the 'stock span' problem work?
It sorts the prices
It keeps indexes of days with higher prices; each day pops lower ones and its span is the distance to the remaining top
It uses a queue
It uses binary search
Sign up free to see the answer and a step-by-step explanation.
Why is a stack used to convert recursion into iteration?
The call stack is itself a stack of frames, so an explicit stack can simulate it
Stacks are faster than recursion always
Recursion needs heaps
Queues can't store data
Sign up free to see the answer and a step-by-step explanation.
What does 'min stack' need to answer getMin() in O(1)?
Scanning the stack
A second stack (or pairs) recording the minimum at each level
A heap
Sorting after each push
Sign up free to see the answer and a step-by-step explanation.
Which problem is a classic use of a stack in compilers?
Matching nested brackets and parsing expressions
Hashing identifiers
Register allocation only
Sorting tokens
Sign up free to see the answer and a step-by-step explanation.
Which traversal of an expression tree gives postfix notation?
Preorder
Inorder
Level order
Postorder
Sign up free to see the answer and a step-by-step explanation.
A recursive function on a list of 100,000 elements hits RecursionError in Python. What is the most robust fix?
Raise the recursion limit to 10⁹
Rewrite it iteratively with an explicit stack
Use a bigger computer
Add a try block
Sign up free to see the answer and a step-by-step explanation.
What is the 'celebrity problem' solved with a stack (or two pointers)?
Sorting people
Shortest paths
Counting friends
Finding the person everyone knows who knows nobody, using O(n) 'knows' questions
Sign up free to see the answer and a step-by-step explanation.