ProgrammingHardTCS CodeVita · Infosys HackWithInfy
What is printed?
What is printed?
import heapq
iv = sorted([(0, 30), (5, 10), (15, 20)])
h = []
for s, e in iv:
if h and h[0] <= s:
heapq.heapreplace(h, e)
else:
heapq.heappush(h, e)
print(len(h))- A. 3
- B. 0
- C. 2
- D. 1
Correct answer: C. 2
This is the minimum-rooms check. The heap holds the end times of rooms in use. Interval (0, 30) needs a room, (5, 10) cannot reuse it so it needs a second, and (15, 20) can reuse the room that ends at 10.
More Python Programming questions
- What is printed? python from collections import Counter a = [1, -1, 2, -2, 0] cnt = Counter({0: 1}) s = 0 ans = 0 for x in a: s += x ans…
- What is printed? python print(4 & 3 == 0, 6 & 3 == 0)
- What is printed? python print(int(1e23), int(1e20))
- What is printed? python x = [3, 1, 2].sort() print(x, sorted([3, 1, 2]))
- What is printed? python print(min([(1, 'b'), (1, 'a')], key=lambda t: t[0]), max(['aa', 'b', 'cc'], key=len))
- What is printed? python d = dict.fromkeys(['a', 'b'], []) d['a'].append(1) print(d)