Assigned Parking
Updated
Problem statement
There are n cars on a grid. Car i is at the point (x[i], y[i]).
You must park all the cars side by side in one horizontal row, with no gaps. So the cars end up at the points (a, b), (a + 1, b), ..., (a + n - 1, b) for some integers a and b that you choose, with one car at each point. Any car may take any of these spots.
Moving a car from (x1, y1) to (x2, y2) uses |x1 - x2| + |y1 - y2| units of fuel.
Return the minimum total fuel needed to park all the cars. Several cars may start at the same point.
Function
minFuel(x: int[], y: int[]) → longExamples
Example 1
x = [1, 4]y = [1, 4]return = 5Park both cars in the row y = 1, at (3, 1) and (4, 1). The car at (1, 1) moves right by 2, and the car at (4, 4) moves down by 3. The total is 2 + 3 = 5.
No plan is cheaper. The two cars are 3 rows apart, so putting them in one row costs at least 3. They are 3 columns apart, but side by side they must be 1 column apart, so that costs at least 2 more.
Example 2
x = [1, 5]y = [1, 5]return = 7Park both cars in the row y = 1, at (4, 1) and (5, 1). The car at (1, 1) moves right by 3, and the car at (5, 5) moves down by 4. The total is 3 + 4 = 7.
Example 3
x = [4, 6, 4, -4, 1]y = [-1, 1, -5, -4, 5]return = 23Park the cars in the row y = -1, at x = 1 to x = 5:
(-4, -4)moves to(1, -1):5 + 3 = 8.(1, 5)moves to(2, -1):1 + 6 = 7.(4, -1)moves to(3, -1):1 + 0 = 1.(4, -5)moves to(4, -1):0 + 4 = 4.(6, 1)moves to(5, -1):1 + 2 = 3.
The total is 8 + 7 + 1 + 4 + 3 = 23, and no other row or start position does better.
Constraints
n == x.length == y.length1 <= n <= 105-109 <= x[i], y[i] <= 109
More Airbnb Online Assessment questions
- Task Management SystemOA · Seen Nov 2026
- Recipe Management SystemOA · Seen Nov 2026
- Progressive Banking SystemOA · Seen Nov 2026
- Credit Accounts with Scheduled TransfersOA · Seen Oct 2026
- Parcel Attribute Store with TTLOA · Seen Oct 2026
- Parcel Event TrackingOA · Seen Oct 2026
- Median Home Price by CityOA · Seen Sep 2026
- In-Memory File SystemOA · Seen Sep 2026
- Worker Management (Promotion Track), Part 4: Full-Shift Double PayOA · Seen Sep 2026
- Worker Management (Promotion Track), Part 3: Promotions and SalaryOA · Seen Sep 2026
Similar questions at other companies
- Maximize Revenue from Decreasing InventoryChubb · OAChubb interview questions
- Return Exact Cash Register ChangeBank of America · OA
- Delete and Freeze GameRubrik · OARubrik interview questions
- Maximum Three-Server Difficulty ScoreVisa · OAVisa interview questions
- Max Transfer RateAmazon · OA
- Apartment Housemate PreferencesGoogle · PHONE SCREENGoogle interview questions
- Fair Electoral Vote ApportionmentBloomberg LP · PHONE SCREENBloomberg LP interview questions
- Minimum Fuel Car AlignmentFivetran · PHONE SCREENFivetran interview questions
- Minimum Rounds to Complete All TasksInfosys · OAInfosys interview questions
- Server Upgrade PlanningWells Fargo · OAWells Fargo interview questions
Interview reports that mention this question
Airbnb interview experiences
- Airbnb SWE OA: Four-Level Banking System: 1 Experience
- Airbnb Intern Phone Interviews: 21 Experiences (Part 7 of 7)
- Airbnb Intern OA and Phone Screens: 17 Experiences (Part 6 of 7)
- Airbnb Intern OA and Onsite Rounds: 16 Experiences (Part 5 of 7)
- Airbnb Intern OA, Diplomacy Game: 19 Experiences (Part 4 of 7)