Question 61
HardWhat is the output of this code?
def jump(a):
jumps = end = far =
i ((a) - ):
far = (far, i + a[i])
i == end:
jumps += ; end = far
jumps
(jump([, , , , ]), jump([, , , ]))
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?
def jump(a):
jumps = end = far =
i ((a) - ):
far = (far, i + a[i])
i == end:
jumps += ; end = far
jumps
(jump([, , , , ]), jump([, , , ]))
2 3
1 3
3 3
2 4
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
def gas(g, c):
if sum(g) < sum(c): return -1
tank = start = 0
for i in range(len(g)):
tank += g[i] - c[i]
if tank < 0: start, tank = i + 1, 0
return start
print(gas([1, 2, 3, 4, 5], [3, 4, 5, 1, 2]), gas([2, 3, 4], [3, 4, 3]))
3 0
4 -1
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
def candy(r):
n = len(r); c = [1] * n
for i in range(1, n):
if r[i] > r[i - 1]: c[i] = c[i - 1] + 1
for i in range(n - 2, -1, -1):
if r[i] > r[i + 1]: c[i] = max(c[i], c[i + 1] + 1)
return sum(c)
print(candy([1, 0, 2]), candy([1, 2, 87, 87, 87, 2, 1]))
4 13
5 11
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
import heapq
def huffman_cost(freq):
h = list(freq); heapq.heapify(h); cost = 0
while len(h) > 1:
a, b = heapq.heappop(h), heapq.heappop(h)
cost += a + b; heapq.heappush(h, a + b)
return cost
print(huffman_cost([5, 9, 12, 13, 16, 45]))
232
100
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
def min_arrows(pts):
pts.sort(key=lambda p: p[1])
arrows, end = 0, float("-inf")
for s, e in pts:
if s > end: arrows += 1; end = e
return arrows
print(min_arrows([[10, 16], [2, 8], [1, 6], [7, 12]]), min_arrows([[1, 2], [3, 4], [5, 6], [7, 8]]))
2 2
2 4
Sign up free to see the answer and a step-by-step explanation.
Why is greedy (largest coin first) optimal for Indian currency denominations but not for {1, 3, 4}?
Greedy is never optimal
The Indian system is 'canonical'; for {1, 3, 4}, 6 = 3 + 3 beats 4 + 1 + 1
{1, 3, 4} has no 1
Greedy is always optimal
Sign up free to see the answer and a step-by-step explanation.
For activity selection, which greedy rule is provably optimal?
Choose the activity that finishes earliest
Choose the one that starts earliest
Choose the one with the fewest overlaps
Choose the shortest activity
Sign up free to see the answer and a step-by-step explanation.
Jobs have deadlines and profits, each taking one unit of time. Which greedy approach maximises profit?
Sort by deadline only
Sort by shortest job
Take jobs in input order
Sort by profit (highest first) and place each job in the latest free slot before its deadline
Sign up free to see the answer and a step-by-step explanation.
What is the minimum total cost of connecting ropes of lengths 4, 3, 2 and 6, where joining two ropes costs their total length?
30
15
25
29
Sign up free to see the answer and a step-by-step explanation.
Which greedy algorithm builds an optimal prefix-free code?
Huffman coding
Shannon–Fano always
Run-length encoding
LZ77
Sign up free to see the answer and a step-by-step explanation.
3 -1
0 -1
5 13
6 14
200
224
1 4
3 4