Question 51
MediumGas stations: total gas ≥ total cost. Where does the greedy method start the circuit?
- 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
Gas stations: total gas ≥ total cost. Where does the greedy method start the circuit?
At the station with most gas
At the station with least cost
After the last point where the running tank went negative
At station 0 always
Sign up free to see the answer and a step-by-step explanation.
What is the minimum number of arrows to burst balloons spanning [10,16], [2,8], [1,6], [7,12]?
4
3
1
2
Sign up free to see the answer and a step-by-step explanation.
What do greedy algorithms rely on besides optimal substructure?
The greedy-choice property: a locally best choice can be part of an optimal solution
Overlapping subproblems
Randomisation
Backtracking
Sign up free to see the answer and a step-by-step explanation.
To minimise the number of meeting rooms for given meetings, which technique works?
Sort by length
Use DFS
Sort by start time and keep a min-heap of end times
Use a stack
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
a = [2, 3, 1, 1, 4]
jumps = end = far = 0
for i in range(len(a) - 1):
far = max(far, i + a[i])
if i == end:
jumps += 1
end = far
print(jumps)
1
3
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
gas, cost = [1, 2, 3, 4, 5], [3, 4, 5, 1, 2]
tank = start = total = 0
for i in range(len(gas)):
total += gas[i] - cost[i]
tank += gas[i] - cost[i]
if tank < 0:
start, tank = i + 1, 0
print(start if total >= 0 else -1)
4
0
Sign up free to see the answer and a step-by-step explanation.
Huffman coding is applied to symbols with frequencies A: 5, B: 9, C: 12, D: 13, E: 16, F: 45. What is the length of F's code?
2 bits
4 bits
3 bits
1 bit
Sign up free to see the answer and a step-by-step explanation.
Which property must a problem have for a greedy algorithm to be correct?
The greedy-choice property and optimal substructure
It must use sorting
Overlapping subproblems only
It must be NP-complete
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
pts = sorted([[10, 16], [2, 8], [1, 6], [7, 12]], key=lambda p: p[1])
arrows, end = 0, float('-inf')
for s, e in pts:
if s > end:
arrows += 1
end = e
print(arrows)
1
3
Sign up free to see the answer and a step-by-step explanation.
Why does greedy coin change fail for coins {1, 3, 4} but work for Indian currency denominations?
Indian denominations form a 'canonical' system where taking the largest coin first is always optimal; {1, 3, 4} is not canonical
Because 4 is even
Greedy never works for coin change
Indian coins are all prime
Sign up free to see the answer and a step-by-step explanation.
4
2
-1
3
2
4