Question 21
EasyRopes of lengths 4, 3, 2 and 6 are joined into one, and joining two ropes costs the sum of their lengths. What is the least total cost?
- 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
Ropes of lengths 4, 3, 2 and 6 are joined into one, and joining two ropes costs the sum of their lengths. What is the least total cost?
29
15
25
30
Sign up free to see the answer and a step-by-step explanation.
Which of these algorithms is NOT greedy?
Kruskal's
Huffman coding
Floyd–Warshall
Prim's
Sign up free to see the answer and a step-by-step explanation.
In what order does Kruskal's algorithm consider the edges?
At random
In increasing order of weight
In decreasing order of weight
In the order they were input
Sign up free to see the answer and a step-by-step explanation.
A triangle has edges of weight 1, 2 and 3. What is the weight of its minimum spanning tree?
1
6
3
5
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
coins = [10, 5, 2, 1]
amount = 18
count = 0
for c in coins:
count += amount // c
amount %= c
print(count)
18
3
Sign up free to see the answer and a step-by-step explanation.
Merging several sorted files two at a time with the least total cost is solved by which approach?
Merge the largest files first
Merge them all at once with recursion
Merge in random order
Greedy: always merge the two smallest files first (optimal merge pattern)
Sign up free to see the answer and a step-by-step explanation.
Children have greed factors [1, 2, 3] and the cookies have sizes [1, 1]. A child is content with a cookie at least as big as its greed. Matching greedily, how many children can be made content?
0
1
3
2
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
acts = [(1, 3), (2, 5), (3, 5), (4, 6), (5, 7)]
acts.sort(key=lambda a: a[1])
end, n = 0, 0
for s, f in acts:
if s >= end:
n += 1
end = f
print(n)
5
2
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
items = [(60, 10), (100, 20), (120, 30)]
cap, total = 50, 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)
240.0
180.0
Sign up free to see the answer and a step-by-step explanation.
Why does greedy work for the fractional knapsack but not for the 0/1 knapsack?
In 0/1 knapsack items can't be split, so taking the best ratio first may waste capacity
Fractional knapsack is NP-hard
Greedy works for both
0/1 knapsack has no weights
Sign up free to see the answer and a step-by-step explanation.
5
4
4
3
160.0
220.0