UBER Coding Question – Solved

4 Live
Fleet Upgrades You're building a system for Uber Fleet Management to optimize vehicle upgrades within a limited operations budget. There are a total of n vehicles where: - `upgradeCost[i]`: cost to upgrade the i-th vehicle - `expectedResale[i]`: expected resale value of the i-th vehicle after one year of operation You may choose to upgrade any vehicle once, and the total upgrade cost must not exceed the given budget. The net gain from upgrading a vehicle is calculated as: `expectedResale[i] - upgradeCost[i]`. Implement a function that determines the maximum net gain achievable by selecting a subset of vehicle upgrades. The function `maximizeFleetGain` takes the following inputs: - `int budget`: the total upgrade budget - `int upgradeCost[]`: an array representing the upgrade costs for the vehicles - `int expectedResale[]`: an array representing the expected resale values after one year The function should return an integer denoting the maximum possible net gain. Example budget = 250 upgradeCost = [175, 133, 109, 210, 97] expectedResale = [200, 125, 128, 228, 133] One optimal selection: - Choose vehicles at index 2 and 4 β†’ upgrade costs = 109 + 97 = 206 - Resale values = 128 + 133 = 261 - Net gain = 261 - 206 = **55** So, the function should return **55**. Constraints - 0 < n ≀ 100 - 0 < budget ≀ 30000 Input Format for Custom Testing Sample Input 30 4 1 2 4 6 5 3 5 6 Sample Output 6 Explanation The optimal strategy is to upgrade all 4 vehicles and achieve net gain of: (5 - 1) + (3 - 2) + (5 - 4) + (6 - 6) = 4 + 1 + 1 + 0 = **6**

Asked in: UBER

Image of the Question

Question Image Question Image Question Image

All Testcases Passed βœ”



Passcode Image

Solution


from functools import lru_cache
def maximizieFleetGain(budget, upgradeCost, expectedResale):
    arr = [(i-j, j) for i,j in zip(expectedResale, upgradeCost) if i-j>0 and j<= budget]
    n = len(arr)
// ... rest of solution available after purchase

πŸ”’ Please login to view the solution

Explanation


```
To solve this problem, you need to think carefully about how to select vehicles for upgrades so that the total upgrade cost does not exceed the given budget, and the total net gain (expected resale minus upgrade cost) is maximized.

---

**Step 1: Understand the Problem and What is Being Asked**

You have `n` vehicles, each with:
- an upgrade cost (upgradeCost[i])
- an expected resale value after one year (expectedResale[i])

You want to choose a subset of these vehicles to upgrade, such that:
- The sum of their upgrade costs does not exceed the budget.
- The sum of their net gains (expectedResale[i] - upgradeCost[i]) is as large as possible.

The goal is to maximize this net gain, not just resale or cost alone.

---

**Step 2: Reformulate the Problem**

The problem essentially boils down to selecting items (vehicles) to maximize total value (net gain), subject to a constraint on total cost (upgrade cost) β€” this is a classic variation of the **0/1 Knapsack problem**.

- Each vehicle is an item.
- The "weight" or "cost" of each item is upgradeCost[i].
- The "value" of each item is netGain[i] = expectedResale[i] - upgradeCost[i].
- The knapsack capacity is the budget.

You want to pick a subset of these "items" such that their total cost is within the budget and total value is maximized.

---

**Step 3: Key Challenges and Considerations**

1. **Negative or zero net gain**: Some vehicles may have net gain ≀ 0, meaning upgrading them may not increase overall gain. It might be optimal to skip these vehicles. Your approach should be flexible to handle this.

2. **Budget constraint**: You cannot exceed the given budget, so careful selection is needed.

3. **Efficient solution required**: Since n ≀ 100 and budget ≀ 30000, a dynamic programming solution with O(n * budget) complexity is feasible.

---

**Step 4: Conceptualizing the Dynamic Programming Approach**

Create a DP structure where:
- The state `dp[i][c]` represents the maximum net gain achievable by considering vehicles up to the i-th vehicle, with total upgrade cost exactly `c`.

You will iteratively build this DP table by deciding for each vehicle whether to upgrade it or skip it:

- If you skip the i-th vehicle, `dp[i][c]` stays the same as `dp[i-1][c]`.
- If you upgrade the i-th vehicle (if upgradeCost[i] ≀ c), then `dp[i][c]` can be updated to the maximum of current value and `dp[i-1][c - upgradeCost[i]] + netGain[i]`.

At the end, the answer will be the maximum value among all `dp[n][c]` where `c` ranges from 0 to budget.

---

**Step 5: Handling Initial Conditions and Boundary Cases**

- Initialize `dp[0][c]` properly: before processing any vehicles, the gain is zero when cost is zero, and negative infinity or some minimal value for other costs since they are invalid.
- Make sure to handle cases where upgrading no vehicle yields zero gain.

---

**Step 6: Final Result**

After filling the DP table, iterate over all possible costs `c` ≀ budget to find the maximum net gain achievable under the budget constraint.

Return this value as the answer.

---

**Step 7: Additional Thoughts**

- Since net gain can be negative, skipping vehicles with negative net gain might be beneficial. The DP formulation inherently handles this by allowing not choosing that vehicle.
- Make sure to use appropriate data types for the DP array to avoid overflow issues.
- To optimize space, consider using a 1D DP array where you update from higher costs to lower costs to avoid overwriting useful data prematurely.

---

**Summary**

This problem is a classic knapsack-type optimization: select vehicles to upgrade maximizing total net gain without exceeding the budget.
The primary direction is to use dynamic programming based on cost constraints, storing and updating maximum net gains for partial solutions, and finally extracting the maximum achievable gain.
This approach is well-suited due to the manageable input size constraints and clearly defined optimization criteria.
```


Related Questions