ProgrammingHardCodeforces-style · placement coding round
What is printed?
What is printed?
import heapq
def rooms(iv):
iv = sorted(iv)
h = []
for s, e in iv:
if h and h[0] <= s:
heapq.heapreplace(h, e)
else:
heapq.heappush(h, e)
return len(h)
print(rooms([(0, 30), (5, 10), (15, 20)]), rooms([(7, 10), (2, 4)]))- A. 1 1
- B. 2 2
- C. 3 1
- D. 2 1
Correct answer: D. 2 1
The heap holds the end times of rooms in use. A meeting reuses a room when the earliest-finishing room is free, so the first list needs 2 rooms and the second needs 1.
More Python Programming questions
- For a Python dict d with n entries, which operation costs O(n) rather than O(1) on average?
- With n = 2 x 10^5 and a 1-second limit in CPython, which algorithm can be rejected because it is too slow?
- A solution loops over 2 x 10^5 queries, and each query runs if x in lst on a list of 2 x 10^5 items. Which change keeps the same answers…
- What is printed? python def tri(t): dp = t[-1][:] for row in range(len(t) - 2, -1, -1): for j in range(len(t[row])): dp[j] = t[row][j] +…
- What is printed? python def lps(s): n = len(s) dp = [[0] n for in range(n)] for i in range(n): dp[i][i] = 1 for L in range(2, n + 1): for…
- What is printed? python fs = [lambda: i for i in range(3)] gs = [lambda i=i: i for i in range(3)] print([f() for f in fs], [g() for g in…