← Meta Interview Insights

Meta·Machine Learning Engineer·Technical Phone Screen·Senior

Senior
Apr 2026

Summary

Meta MLE coding round, just one algorithmic problem but it was a tricky greedy one that I hadn't thought about in a while. Felt okay about my solution but not confident I nailed the optimal approach right away.

Questions Asked (1)

Q1

A car starts at position 0 and needs to reach a destination `target` miles away. Gas stations are scattered along the route, each with a known position and fuel amount. The car begins with `startFuel` liters and consumes 1 liter per mile. You can stop at any station to take all its fuel. What is the minimum number of refueling stops needed to reach the destination, or -1 if it's impossible?

Algorithms & Data Structures
Author's notes

I knew this was a greedy problem pretty quickly but fumbled the implementation for a bit.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Use a greedy strategy with a max-heap to always refuel at the station with the most fuel among those passed when the car runs out of gas. Iterate through stations sorted by position, adding reachable stations' fuel to the heap, and when fuel is insufficient to reach the next station or target, pop the max fuel and increment stops. If the heap is empty and fuel is insufficient, return -1.

Pro tip: Clarify that the greedy choice is optimal because refueling at the station with maximum fuel among reachable ones minimizes the number of stops, and mention that this is a classic problem (LeetCode 871) often asked at Meta.

1. Understand the problem and constraints

Restate the problem: car starts at 0 with startFuel, stations have positions and fuel amounts, need minimum refueling stops to reach target. Note that fuel consumption is 1 liter per mile, and you can only refuel at stations you pass.

2. Choose the right data structures

Use a max-heap (priority queue) to store fuel amounts of stations that have been passed but not yet used. Sort stations by position to process them in order.

3. Simulate the journey with greedy refueling

Iterate through stations and target as checkpoints. At each checkpoint, if current fuel is insufficient to reach it, repeatedly pop the max fuel from the heap and add to current fuel, incrementing stop count, until fuel is enough or heap is empty. If heap is empty and still insufficient, return -1.

4. Handle edge cases and return result

If target is reached, return the number of stops. Consider cases like startFuel already enough (return 0), no stations, or unreachable target. Ensure stations beyond target are ignored.

5. Analyze complexity and test

Time complexity: O(n log n) due to sorting and heap operations. Space complexity: O(n) for the heap. Walk through a small example to verify correctness.

Key Points to Mention

  • Greedy approach: always refuel at the station with maximum fuel among reachable ones to minimize stops.
  • Use a max-heap to efficiently retrieve the maximum fuel available.
  • Sort stations by position to process them in order.
  • Time complexity O(n log n) and space complexity O(n).
  • Edge cases: startFuel >= target, no stations, unreachable target.
  • Proof of optimality: greedy choice property and exchange argument.

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.