Bird
Raised Fist0
Interview Prepgreedy-algorithmsmediumAmazonFacebookBloomberg

Gas Station (Circular)

Choose your preparation mode4 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Steps
setup

Calculate net gas at each station

Compute net gas array where each element is gas[i] - cost[i]. This shows how much gas is gained or lost at each station.

💡 Net gas array simplifies the problem by focusing on surplus or deficit at each station.
Line:net = [gas[i] - cost[i] for i in range(n)]
💡 Net gas array reveals which stations add or consume gas, critical for checking circuit feasibility.
📊
Gas Station (Circular) - Watch the Algorithm Execute, Step by Step
Watching each step reveals how the greedy approach efficiently checks feasibility without simulating the entire trip repeatedly.
Step 1/18
·Active fillAnswer cell
record
-2
0
-2
1
-2
2
3
3
3
4
compare
-2
0
-2
1
-2
2
3
3
3
4
Result: 0
record
0
0
0
1
0
2
0
3
0
4
0
5
0
6
0
7
0
8
0
9
0
10
record
i
0
0
-2
1
0
2
0
3
0
4
0
5
0
6
0
7
0
8
0
9
0
10
record
0
0
i
-2
1
-4
2
0
3
0
4
0
5
0
6
0
7
0
8
0
9
0
10
record
0
0
-2
1
i
-4
2
-6
3
0
4
0
5
0
6
0
7
0
8
0
9
0
10
record
0
0
-2
1
-4
2
i
-6
3
-3
4
0
5
0
6
0
7
0
8
0
9
0
10
record
0
0
-2
1
-4
2
-6
3
i
-3
4
0
5
0
6
0
7
0
8
0
9
0
10
record
0
0
-2
1
-4
2
-6
3
-3
4
i
0
5
-2
6
0
7
0
8
0
9
0
10
record
0
0
-2
1
-4
2
-6
3
-3
4
0
5
i
-2
6
-4
7
0
8
0
9
0
10
record
0
0
-2
1
-4
2
-6
3
-3
4
0
5
-2
6
i
-4
7
-6
8
0
9
0
10
record
0
0
-2
1
-4
2
-6
3
-3
4
0
5
-2
6
-4
7
i
-6
8
-3
9
0
10
record
0
0
-2
1
-4
2
-6
3
-3
4
0
5
-2
6
-4
7
-6
8
i
-3
9
0
10
compare
i
0
0
-2
1
-4
2
-6
3
-3
4
0
5
-2
6
-4
7
-6
8
-3
9
0
10
compare
0
0
i
-2
1
-4
2
-6
3
-3
4
0
5
-2
6
-4
7
-6
8
-3
9
0
10
compare
0
0
-2
1
i
-4
2
-6
3
-3
4
0
5
-2
6
-4
7
-6
8
-3
9
0
10
compare
0
0
-2
1
-4
2
i
-6
3
-3
4
0
5
-2
6
-4
7
-6
8
-3
9
0
10
Result: 3
record
0
0
-2
1
-4
2
start
-6
3
-3
4
0
5
-2
6
-4
7
-6
8
-3
9
0
10
Result: 3

Key Takeaways

The net gas array transforms the problem into finding a non-negative sum subarray in a circular array.

This insight is hard to see from code alone because it abstracts the problem into a simpler numeric form.

Prefix sums allow O(1) queries for sums over any window, enabling efficient feasibility checks.

Visualizing prefix sums clarifies why the algorithm avoids repeated summations.

Checking each start index with prefix sums reveals exactly where the circuit can be completed.

Seeing each comparison step shows why some start points fail and one succeeds.

Practice

(1/5)
1. You have a list of children each with a greed factor and a list of cookies each with a size. You want to assign cookies to children so that each child gets at most one cookie and the cookie size is at least the child's greed factor. Which algorithmic approach guarantees the maximum number of content children?
easy
A. Greedy algorithm by sorting greed factors and cookie sizes, then assigning smallest sufficient cookie to each child
B. Dynamic Programming to try all possible assignments and pick the best
C. Brute force nested loops checking every cookie for every child without sorting
D. Divide and Conquer by splitting children and cookies and merging results

Solution

  1. Step 1: Understand problem constraints

    Each child can get at most one cookie, and the cookie must satisfy the child's greed factor.
  2. Step 2: Identify optimal approach

    Sorting both greed and cookie arrays allows a greedy assignment from smallest greed to smallest sufficient cookie, ensuring maximum matches.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Greedy sorting approach is classic for assignment problems [OK]
Hint: Sort both arrays and assign greedily [OK]
Common Mistakes:
  • Thinking brute force is needed for optimality
  • Assuming DP is required
  • Ignoring sorting leads to suboptimal matches
2. You have a list of tasks represented by characters, each task takes 1 unit of time to execute. The CPU must wait for at least n units of time before executing the same task again. Which approach guarantees the minimum total time to finish all tasks?
easy
A. Dynamic Programming that tries all permutations of task orders to find the minimal schedule
B. Greedy algorithm using a max-heap to always schedule the most frequent available task next
C. Simple round-robin scheduling without considering cooldown intervals
D. Sorting tasks by frequency and inserting idle slots greedily without priority queue

Solution

  1. Step 1: Understand the cooldown constraint

    The CPU must wait n units before repeating the same task, so scheduling must consider task frequencies and cooldowns.
  2. Step 2: Why max-heap greedy works best

    Using a max-heap prioritizes tasks with the highest remaining frequency, ensuring minimal idle time by always picking the most urgent task available.
  3. Final Answer:

    Option B -> Option B
  4. Quick Check:

    Max-heap approach matches known optimal solution [OK]
Hint: Max-heap greedily schedules highest frequency tasks first [OK]
Common Mistakes:
  • Assuming DP or brute force is needed
  • Ignoring cooldown leads to incorrect minimal time
  • Greedy without priority queue misses optimal order
3. Identify the bug in the following code snippet for Two City Scheduling:
def twoCitySchedCost(costs):
    costs.sort(key=lambda x: abs(x[0] - x[1]))
    n = len(costs) // 2
    total = 0
    for i, cost in enumerate(costs):
        if i < n:
            total += cost[0]
        else:
            total += cost[1]
    return total
medium
A. Sorting by absolute difference instead of signed difference
B. Incorrect calculation of n as half the length
C. Adding cost[1] for the first half instead of cost[0]
D. Using enumerate instead of a simple for loop

Solution

  1. Step 1: Analyze sorting key

    The code sorts by absolute difference abs(cost[0] - cost[1]) instead of signed difference cost[0] - cost[1].
  2. Step 2: Effect of wrong sorting

    Sorting by absolute difference loses the direction of cost preference, causing suboptimal assignments and higher total cost.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Signed difference sorting is required for correct greedy assignment [OK]
Hint: Sort by signed difference, not absolute [OK]
Common Mistakes:
  • Sorting by absolute difference
  • Miscounting half the people
  • Swapping city costs in assignment
4. Suppose the problem changes: you can reuse indices multiple times (cycles allowed). Which modification to the BFS approach ensures correct minimum jumps without infinite loops?
hard
A. Remove the visited set to allow revisiting indices for better paths.
B. Keep the visited set but reset it after each BFS level to allow revisits in next level.
C. Keep the visited set as is to prevent revisiting and infinite loops.
D. Use a distance array to track minimum jumps to each index and update it if a shorter path is found.

Solution

  1. Step 1: Understand cycles and revisits

    Allowing reuse means indices can be revisited, so visited set alone blocks better paths.
  2. Step 2: Use distance array for shortest paths

    Tracking minimum jumps per index and updating when a shorter path is found prevents infinite loops and finds optimal jumps.
  3. Step 3: Why other options fail

    Removing visited causes infinite loops; resetting visited loses pruning; keeping visited blocks revisits.
  4. Final Answer:

    Option D -> Option D
  5. Quick Check:

    Distance array with updates handles cycles correctly [OK]
Hint: Distance array needed when revisits allowed [OK]
Common Mistakes:
  • Removing visited set causing infinite loops
  • Resetting visited incorrectly
  • Assuming original BFS works unchanged
5. Suppose the wiggle subsequence problem is modified so that elements can be reused multiple times in the subsequence (i.e., repeated picks allowed). Which of the following statements about adapting the optimal greedy algorithm is correct?
hard
A. A modified greedy approach can work by allowing zero differences and counting repeated elements
B. The original greedy algorithm works without changes because reuse does not affect difference signs
C. Reuse breaks the problem definition, so no efficient algorithm exists
D. The algorithm must be changed to a dynamic programming approach to handle reuse correctly

Solution

  1. Step 1: Understand reuse impact

    Allowing reuse means subsequence elements can repeat, changing the problem fundamentally and invalidating the original greedy assumptions.
  2. Step 2: Identify correct adaptation

    Greedy approach relies on strictly alternating differences without reuse; to handle reuse, a dynamic programming approach that tracks states and counts is needed.
  3. Final Answer:

    Option D -> Option D
  4. Quick Check:

    Reuse requires more complex state tracking than greedy allows [OK]
Hint: Reuse breaks greedy assumptions; DP needed
Common Mistakes:
  • Assuming greedy still works
  • Thinking reuse is trivial
  • Ignoring problem definition changes