Question 31
MediumWhat is the output of this code?
a = [2, 3, 1, 1, ]
reach =
i, x (a):
i > reach:
reach = (reach, i + x)
(reach >= (a) - )
Popular companies
Prepare company by company
Hiring pattern, syllabus, past questions and mocks for each company.
All companiesPractice by subject
Data Structures and Algorithms
What is the output of this code?
a = [2, 3, 1, 1, ]
reach =
i, x (a):
i > reach:
reach = (reach, i + x)
(reach >= (a) - )
False
Error
True
None
Sign up free to see the answer and a step-by-step explanation.
What is the time complexity of Kruskal's algorithm with union–find?
O(E × V)
O(V²)
O(V + E)
O(E log E)
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
arr = [900, 940, 950, 1100]
dep = [910, 1200, 1120, 1130]
arr.sort(); dep.sort()
i = j = cur = best = 0
while i < len(arr):
if arr[i] <= dep[j]:
cur += 1; i += 1
else:
cur -= 1; j += 1
best = max(best, cur)
print(best)
4
3
Sign up free to see the answer and a step-by-step explanation.
What is the time complexity of the greedy activity-selection algorithm for n activities?
O(n log n), dominated by sorting by finish time
O(2ⁿ)
O(n²)
O(log n)
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
acts = [(1, 4), (3, 5), (0, 6), (5, 7), (3, 9), (5, 9), (6, 10), (8, 11), (8, 12), (2, 14), (12, 16)]
acts.sort(key=lambda a: a[1])
end, chosen = -1, []
for s, f in acts:
if s >= end:
chosen.append((s, f)); end = f
print(len(chosen), chosen[-1])
4 (8, 12)
4 (12, 16)
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
items = [(480, 30), (400, 20), (180, 10)]
cap, total = 40, 0.0
for v, w in sorted(items, key=lambda x: x[0] / x[1], reverse=True):
take = min(w, cap)
total += v * take / w
cap -= take
print(total)
1060.0
880.0
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
def coins(n, cs=(2000, 500, 200, 100, 50, 20, 10, 5, 2, 1)):
cnt = 0
for c in cs:
cnt += n // c
n %= c
return cnt
print(coins(2738))
7
8
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
jobs = [("a", 2, 100), ("b", 1, 19), ("c", 2, 27), ("d", 1, 25), ("e", 3, 15)]
slots = [None] * 3
for name, dl, p in sorted(jobs, key=lambda j: -j[2]):
for t in range(dl - 1, -1, -1):
if slots[t] is None:
slots[t] = name
break
print(slots)
['a', 'c', 'e']
['d', 'a', 'e']
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
iv = [(1, 3), (2, 6), (8, 10), (9, 12), (15, 18)]
out = []
for s, e in sorted(iv):
if out and s <= out[-1][1]:
out[-1][1] = max(out[-1][1], e)
else:
out.append([s, e])
print(out)
[[1, 3], [2, 6], [8, 12], [15, 18]]
[[1, 12], [15, 18]]
Sign up free to see the answer and a step-by-step explanation.
What is the minimum number of platforms needed for trains arriving at 9:00, 9:40, 9:50, 11:00, 15:00, 18:00 and departing at 9:10, 12:00, 11:20, 11:30, 19:00, 20:00?
4
6
3
2
Sign up free to see the answer and a step-by-step explanation.
1
2
5 (12, 16)
3 (8, 11)
580.0
740.0
9
6
['c', 'a', 'e']
['b', 'a', 'e']
[[1, 6], [8, 10], [9, 12], [15, 18]]
[[1, 6], [8, 12], [15, 18]]