ProgrammingVery hardAmazon OA · Flipkart coding round
What is printed?
What is printed?
def remove_k(num, k):
st = []
for d in num:
while k and st and st[-1] > d:
st.pop()
k -= 1
st.append(d)
st = st[:len(st) - k] if k else st
return ''.join(st).lstrip('0') or '0'
print(remove_k('1432219', 3), remove_k('10200', 1), remove_k('10', 2))- A. 1219 1200 0
- B. 1432 200 0
- C. 1219 20 0
- D. 1219 200 0
Correct answer: D. 1219 200 0
A monotonic stack removes a digit whenever a smaller digit arrives, which keeps the number as small as possible. Leftover deletions come from the end, and leading zeros are stripped, so '10' with two deletions gives 0.
More Python Programming questions
- What is printed? python def divcount(a, k): cnt = {0: 1} s = 0 ans = 0 for x in a: s = (s + x) % k ans += cnt.get(s, 0) cnt[s] =…
- What is printed? python print(sorted(['b', 'A', 'a', 'B']), sorted(['b', 'A', 'a', 'B'], key=str.lower))
- What is printed? python print('a-b-c-d'.rsplit('-', 2), 'a-b-c-d'.split('-', 1))
- What is printed? python print('²'.isdigit(), '²'.isdecimal(), '٣'.isdecimal())
- What is printed? python m = ['abc', 'def'] print([''.join(t) for t in zip(m)])
- What is printed? python print(list(zip([[1, 2, 3], [4, 5]])), list(zip([[1, 2], [3, 4, 5]], strict=False)))