FastPrepMinimum Fuel Car Alignment
Problems/Minimum Fuel Car Alignment

Minimum Fuel Car Alignment

Fivetran logoFivetran● MediumFULLTIMEPHONE SCREEN

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: The report gives the goal of lining cars up horizontally and the Manhattan fuel cost. It does not give limits on the coordinates or details of how cars are assigned. Here the coordinates are integers and cars are reassigned in the best way to consecutive spots. Our version is about 92% the same as the reported question.

You are given the positions of n cars. Car i is at (xCoordinates[i], yCoordinates[i]), and every coordinate is an integer.

Move every car so that the cars end up side by side in one horizontal row, with no gaps. You choose the integer row y and the integer starting position s. The cars then fill the spots (s, y), (s + 1, y), ..., (s + n - 1, y), one car per spot, and you decide which car goes to which spot.

Moving a car from (x1, y1) to (x2, y2) costs |x1 - x2| + |y1 - y2| units of fuel.

Return the smallest possible total fuel cost. The answer is a 64-bit integer.

Function

minimumFuelCost(xCoordinates: int[], yCoordinates: int[]) → long

Examples

Example 1

xCoordinates = [1,4]yCoordinates = [1,4]return = 5

Put the row at y = 1 and use the x-positions [1, 2]. The first car is already in place, so it costs 0. The second car costs |4 - 2| + |4 - 1| = 5. No other layout is cheaper, so the answer is 5.

Example 2

xCoordinates = [0,5,2]yCoordinates = [2,2,2]return = 3

All cars are already in row y = 2, so keep that row. Use the x-positions [1, 2, 3]. Sorted, the cars' x-coordinates are [0, 2, 5]. Match them in that order: the moves cost 1 + 0 + 2 = 3, which is the answer.

Example 3

xCoordinates = [4,1,3,2]yCoordinates = [3,0,2,1]return = 4

The x-coordinates already fill four spots in a row with no gap, so no car moves sideways. Put the row at y = 1. The up and down moves cost 2 + 1 + 1 + 0 = 4, which is the answer.

Constraints

  • 1 <= n <= 2 * 10^5
  • xCoordinates.length == yCoordinates.length == n
  • -10^9 <= xCoordinates[i], yCoordinates[i] <= 10^9
  • Every final coordinate is an integer, and each of the n consecutive final positions is occupied by exactly one car.
  • The minimum total cost fits in a signed 64-bit integer.

More Fivetran interview questions

  • Execute In-Memory SQL Storage OperationsPHONE SCREEN · Seen Sep 2026
  • Aladdin and the Magic CarpetPHONE SCREEN · Seen Jun 2025
  • Reschedule Meetings for Maximum Free TimeOA · Seen Nov 2026
  • Connected GroupsONSITE INTERVIEW · Seen Jun 2024
  • Unique Near-Equal Target-Sum PairsONSITE INTERVIEW · Seen Jun 2024

Similar questions at other companies

  • Maximize Revenue from Decreasing InventoryChubb · OAChubb interview questions
  • Delete and Freeze GameRubrik · OARubrik interview questions
  • Maximum Three-Server Difficulty ScoreVisa · OAVisa interview questions
  • Fair Capped Money DistributionGusto · PHONE SCREEN · ONSITE INTERVIEWGusto interview questions
  • Tennis Court SchedulingAtlassian · PHONE SCREEN · ONSITE INTERVIEWAtlassian interview questions
  • Apartment Housemate PreferencesGoogle · PHONE SCREENGoogle interview questions
  • Adaptive Calibration ErrorOpenAI · PHONE SCREENOpenAI interview questions
  • Maximum Number of Events That Can Be AttendedByteDance · PHONE SCREENByteDance interview questions
  • Remove the Fewest Magic BeansThe D.E. Shaw Group · PHONE SCREENThe D.E. Shaw Group interview questions
  • Aggressive CowsAmazon · PHONE SCREEN

Fivetran interview experiences

  • Fivetran Staff SRE Hiring Manager Interview: 1 Experience
  • Fivetran Software Engineer HackerRank and Java: 1 Experience
  • Fivetran Senior SRE Interview Process: 1 Experience
  • Fivetran Software Engineer HashMap and Design: 1 Experience
  • Fivetran SDE-2 Coding and Parking Lot Design: 1 Experience
  • Fivetran Software Engineer Interview, Toronto: 1 Experience
All Fivetran interview questionsSee Fivetran hiring insights
Saved
class Solution {
    public long minimumFuelCost(int[] xCoordinates, int[] yCoordinates) {
        // write your code here
    }
}
Checking account…