Question 21
EasyWhich Python structure is an efficient stack?
- 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
Which Python structure is an efficient stack?
A tuple
A list using append() and pop()
A string
A list using insert(0, x) and pop(0)
Sign up free to see the answer and a step-by-step explanation.
Which Java class is recommended for a stack in modern code?
ArrayDeque
TreeSet
HashMap
Vector
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
s = []
for x in [1, 2, 3, 4]:
s.append(x)
s.pop()
s.pop()
print(s[-1])
4
1
Sign up free to see the answer and a step-by-step explanation.
Which technique finds the next greater element for every item of an array in O(n)?
A monotonic stack
Two nested loops
A min-heap
Binary search
Sign up free to see the answer and a step-by-step explanation.
What is the postfix form of the infix expression A * B + C?
A B * C +
A B C * +
A B C + *
Sign up free to see the answer and a step-by-step explanation.
What is the time complexity of checking whether a string of n brackets is balanced?
O(n)
O(1)
O(n²)
O(log n)
Sign up free to see the answer and a step-by-step explanation.
Starting with an empty stack, you push 5 items, pop 2, then push 1 more. How many items are on the stack?
6
4
8
3
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
def ok(s):
st, pairs = [], {")": "(", "]": "[", "}": "{"}
for ch in s:
if ch in "([{":
st.append(ch)
elif not st or st.pop() != pairs[ch]:
return False
return not st
print(ok("({[]})"), ok("([)]"))
True True
False False
Sign up free to see the answer and a step-by-step explanation.
What is the postfix form of the infix expression A + B * C?
A + B C *
A B C * +
A B + C *
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
tokens = "2 3 4 * +".split()
st = []
for t in tokens:
if t in "+-*":
b, a = st.pop(), st.pop()
st.append(a + b if t == "+" else a - b if t == "-" else a * b)
else:
st.append(int(t))
print(st[0])
14
20
Sign up free to see the answer and a step-by-step explanation.
3
2
False True
True False
11
9