Data Structures & AlgorithmsHardProduct Company SDE · stack OA
Problem: What is printed by the stack-based decoder for '3[a2[c]]'? It expands k[s] to s repeated k times.
Problem: What is printed by the stack-based decoder for '3[a2[c]]'? It expands k[s] to s repeated k times.
def decode(s):
stack = []
cur, num = '', 0
for ch in s:
if ch.isdigit():
num = num * 10 + int(ch)
elif ch == '[':
stack.append((cur, num))
cur, num = '', 0
elif ch == ']':
prev, k = stack.pop()
cur = prev + cur * k
else:
cur += ch
return cur
print(decode('3[a2[c]]'))What is printed?- A. aaccc
- B. accacc
- C. accaccacc
- D. acccacc
Correct answer: C. accaccacc
Inner 2[c] gives cc, then a + cc = acc, and repeating three times gives accaccacc. Reversing the repeat order gives the trap acccacc.
More Coding Interview Problems questions
- Problem: What is the minimum number of swaps needed to sort the permutation [4, 3, 2, 1] (values 1..4 each once)? Use the cycle…
- Problem: What is floor(sqrt(2000)) found by binary search? Run the code. python lo, hi = 0, 2000 while lo < hi: mid = (lo + hi + 1) // 2…
- Problem: Tasks with (duration, deadline) are (3,5), (2,4), (4,9), (1,3). Scheduling by earliest deadline first, how many tasks finish on…
- Problem: Which statement about the greedy algorithm for 'minimum number of lecture rooms' (interval partitioning) is correct?
- Problem: Which statement about bitmask DP for a Hamiltonian path problem on n = 20 vertices is correct in the standard sense?
- Problem: Which claim about a binary search over a rotated sorted array (no duplicates) finding the minimum element is correct?