ProgrammingVery hardTCS CodeVita · Infosys HackWithInfy
What is printed?
What is printed?
import heapq
rooms = []
for start, end in sorted([(1, 4), (2, 3), (3, 5)]):
if rooms and rooms[0] <= start:
heapq.heapreplace(rooms, end)
else:
heapq.heappush(rooms, end)
print(len(rooms), sorted(rooms))- A. 2 [3, 5]
- B. 2 [4, 5]
- C. 3 [3, 4, 5]
- D. 1 [5]
Correct answer: B. 2 [4, 5]
The interval (1, 4) starts a room. (2, 3) overlaps it, so a second room is needed. (3, 5) can reuse the room that ended at 3, which is replaced by 5, so the heap holds 4 and 5.
More Python Programming questions
- What is printed? python print(' '.join(str(x) for x in range(3, 0, -1)), ' '.join(map(str, [])) == '')
- What is printed? python def gensquares(n): for i in range(n): yield i i print(list(gensquares(4)), sum(gensquares(4)), max(gensquares(0)…
- What is printed? python x = 255 print(f'{x:x}', f'{x:08b}', f'{x:o}', f'{x:#06x}')
- What is printed? python print(3 in {1: 'a', 3: 'b'}, '3' in {1: 'a', 3: 'b'}, 3 in [1, 3], 3 in range(3, 4))
- What is printed? python print(list(zip('ab', 'cd', 'ef'))[1], dict(zip('ab', [1])))
- What is printed? python print(len('héllo'), len('héllo'.encode('utf-8')), 'é'.encode('utf-8'))