Embercode
✕
Greedy AlgorithmsQuick quiz

Gas station in Python

6 questions. Covers Gas station: circular greedy in Python.

Questions 1 to 5 of 6

  1. 1. In the gas station scan, the running balance goes below zero at station i. Which station does the scan try as the next start?
  2. 2. What does this code print? ``python def no_reset(gas, cost): n = len(gas) total = 0 tank = 0 start = 0 for i in range(n): diff = gas[i] - cost[i] total += diff tank += diff if tank < 0: start = i + 1 if total < 0: return -1 return start % n print(no_reset([1, 2, 3, 4, 5], [3, 4, 5, 1, 2])) ``
  3. 3. Why does can_complete_circuit check total < 0 before it returns start?
  4. 4. True or False: the start that the scan returns is the only station that can finish the lap.
  5. 5. True or False: the brute-force check needs % n to wrap the trip around the circle.

5 question(s) left on this page.