Question 31
MediumWhat is the output of this code?
a = [4, 5, 10, 2]
res = [-] * (a)
st = []
i, x (a):
st a[st[-]] < x:
res[st.pop()] = x
st.append(i)
(res)
Popular companies
Prepare company by company
Hiring pattern, syllabus, past questions and mocks for each company.
All companiesPractice by subject
Data Structures and Algorithms
What is the output of this code?
a = [4, 5, 10, 2]
res = [-] * (a)
st = []
i, x (a):
st a[st[-]] < x:
res[st.pop()] = x
st.append(i)
(res)
[-1, -1, 5, 10]
[5, 10, 10, -1]
[5, 10, -1, -1]
[10, -1, -1, -1]
Sign up free to see the answer and a step-by-step explanation.
How can a queue be implemented using two stacks?
It can't be done
Push to an 'in' stack; pop from an 'out' stack, refilling it from 'in' only when it is empty
Use a stack of size 2
Use one stack and reverse it on every operation
Sign up free to see the answer and a step-by-step explanation.
How can a stack support getMin() in O(1)?
Keep a second stack holding the current minimum after each push
Scan the stack each time
Sort the stack after each push
Use a queue instead
Sign up free to see the answer and a step-by-step explanation.
In the shunting-yard algorithm that converts infix to postfix, what is held on the stack?
Operands only
Operators and opening brackets
The finished postfix expression
Nothing; it uses a queue
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
def ok(s):
st, pair = [], {")": "(", "]": "[", "}": "{"}
for c in s:
if c in "([{":
st.append(c)
elif not st or st.pop() != pair[c]:
return False
return not st
print(ok("{[()]}"), ok("([)]"), ok("(("))
True True False
True False True
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
def evalp(tokens):
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 if t == "*" else a // b)
else:
st.append(int(t))
return st[0]
print(evalp("5 1 2 + 4 * + 3 -".split()))
12
14
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
t = [73, 74, 75, 71, 69, 72, 76, 73]
res, st = [0] * len(t), []
for i, x in enumerate(t):
while st and t[st[-1]] < x:
j = st.pop()
res[j] = i - j
st.append(i)
print(res)
[1, 1, 4, 2, 1, 0, 0, 0]
[1, 1, 3, 2, 1, 1, 0, 0]
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
st, mins = [], []
def push(x):
st.append(x)
mins.append(x if not mins else min(x, mins[-1]))
def pop():
st.pop(); mins.pop()
for x in [5, 3, 7, 2]:
push(x)
pop()
print(mins[-1], st[-1])
2 7
3 2
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
h = [2, 1, 5, 6, 2, 3]
st, best = [], 0
for i, x in enumerate(h + [0]):
start = i
while st and st[-1][1] > x:
j, y = st.pop()
best = max(best, y * (i - j))
start = j
st.append((start, x))
print(best)
6
8
Sign up free to see the answer and a step-by-step explanation.
What is the postfix form of A * (B + C) − D / E?
A B C + * D E / −
A B C * + D E − /
A B + C * D E / −
Sign up free to see the answer and a step-by-step explanation.
False False False
True False False
18
20
[0, 0, 4, 2, 1, 1, 0, 0]
[1, 1, 4, 2, 1, 1, 0, 0]
3 7
5 7
10
12