UBER Coding Question – Solved

12 Live
Q. Package Drop Optimization Uber Connect's package delivery team is reviewing how delivery agents handle packages across multiple zones. For each of the n delivery zones: - `scheduledDrop[i]` stores the scheduled time (in minutes) to drop the package in the i-th zone. - `realDrop[i]` stores the actual time (in minutes) the package was dropped in the i-th zone. Due to system limitations, the assignment of scheduled times and actual times to zones was not fixed, and can be shuffled independently before analysis. To evaluate overall performance, Uber calculates a weighted drop delay, where each zone is given a weight equal to its zone number. The weighted drop delay is formally defined as: Total Weighted Drop Delay = ∑ i * (realDrop[i] - scheduledDrop[i]), where i is from 1 to n (1-based indexing). Implement a function that determines the maximum possible weighted drop delay defined above. You are allowed to reorder both arrays independently in any way to maximize the total delay score. The function `maximizeDropDelay` takes the following parameters: - `int scheduledDrop[]`: the scheduled drop times (in minutes) - `int realDrop[]`: the actual drop times (in minutes) The function should return an integer representing the maximum possible weighted drop delay. Since the answer can be very large, return the answer modulo (10⁹ + 7). Note: The arrays use 1-based indexing in the formula, but are given as 0-based arrays in implementation. Example n = 4 scheduledDrop = [2, 1, 3, 4] realDrop = [2, 3, 2, 3] Some of the possible rearrangements and their resulting weighted drop delay scores: Weighted Drop Delay = ∑ i * (realDrop[i] - scheduledDrop[i]) It is guaranteed that no other possible rearrangement of the arrays will generate a weighted drop delay greater than 7. Hence, the answer is **7**. Constraints - 1 ≤ n ≤ 10⁵ - 1 ≤ scheduledDrop[i] ≤ 10⁹ - 1 ≤ realDrop[i] ≤ 10⁹ Sample Input 3 1 2 3 10 10 10 Sample Output 50 Explanation It is optimal to rearrange scheduledDrop to: [3, 2, 1] and keep realDrop the same. Calculating weighted drop delay for each index: - i = 1 → 1 × (10 - 3) = 7 - i = 2 → 2 × (10 - 2) = 16 - i = 3 → 3 × (10 - 1) = 27 Total = 7 + 16 + 27 = 50, which is the maximum possible weighted drop delay. Hence, the answer is 50.

Asked in: UBER

Image of the Question

Question Image Question Image Question Image Question Image

All Testcases Passed ✔



Passcode Image

Solution


def maximiZeDropDelay(scheduleDrop, realDrop):
    ans = 0
    scheduleDrop.sort()
    realDrop.sort(reverse = True)
// ... rest of solution available after purchase

🔒 Please login to view the solution

Explanation


```
To approach this problem, let's carefully analyze what is being asked and what variables we can control to maximize the weighted drop delay.

The key formula to maximize is:
Total Weighted Drop Delay = ∑ i * (realDrop[i] - scheduledDrop[i]) for i from 1 to n.

Here, i is the zone number (1-based index), realDrop[i] is the actual drop time assigned to zone i, and scheduledDrop[i] is the scheduled drop time assigned to zone i.

Important points to note:
- We are allowed to reorder both scheduledDrop and realDrop arrays independently before pairing them by index to calculate the weighted drop delay.
- Our goal is to maximize the sum of weighted differences multiplied by their zone indices.
- Because the arrays are large (up to 10^5 elements), an efficient approach is needed.
- The answer must be modulo 10^9+7 due to potentially large values.

---

**Step 1: Understanding the Problem Intuitively**

We want to pair elements from the scheduledDrop and realDrop arrays to maximize the sum:
∑ i * (realDrop[i] - scheduledDrop[i]).

Since i is fixed by the final order of the arrays after rearrangement, and we are free to permute scheduledDrop and realDrop independently, the problem boils down to how to pair elements optimally.

Rewrite the sum as:
∑ i * realDrop[i] - ∑ i * scheduledDrop[i].

The terms i * realDrop[i] and i * scheduledDrop[i] depend on how we assign values to indices i.

Our freedom:
- We can reorder scheduledDrop arbitrarily.
- We can reorder realDrop arbitrarily.

Our goal is to find permutations of scheduledDrop and realDrop that maximize the difference of weighted sums:
(∑ i * realDrop[i]) - (∑ i * scheduledDrop[i]).

---

**Step 2: Applying Rearrangement Inequality**

This is a classic scenario for applying the rearrangement inequality, which states that to maximize the sum of products of two sequences, both sequences should be sorted in the same order (both ascending or both descending). To minimize the sum of products, one should sort one sequence ascending and the other descending.

In our problem, we want to maximize:
∑ i * realDrop[i] - ∑ i * scheduledDrop[i] = (∑ i * realDrop[i]) - (∑ i * scheduledDrop[i]).

Since i is fixed (1 to n in order), we can think about how to assign the scheduledDrop and realDrop values to indices i optimally.

Note: We do not reorder i, only the arrays scheduledDrop and realDrop.

Imagine sequences:
- weights = [1, 2, 3, ..., n] (fixed ascending order)
- scheduledDrop: we want to assign to positions to minimize ∑ i * scheduledDrop[i] to maximize the total difference.
- realDrop: we want to assign to positions to maximize ∑ i * realDrop[i].

Therefore:
- Assign the smallest scheduledDrop values to the largest weights (positions with large i) to minimize ∑ i * scheduledDrop[i].
- Assign the largest realDrop values to the largest weights to maximize ∑ i * realDrop[i].

---

**Step 3: Determining the Ordering**

So, for maximum weighted drop delay:
- Sort scheduledDrop in ascending order and assign them to positions in descending order of i (largest i gets smallest scheduledDrop).
- Sort realDrop in ascending order and assign them to positions in ascending order of i (largest i gets largest realDrop).

Or equivalently:
- For scheduledDrop, assign the smallest values to the largest weights (positions with large i).
- For realDrop, assign the largest values to the largest weights.

Hence, the final pairing is:
- scheduledDrop sorted ascending → assigned to positions with i descending (index n, n-1, ..., 1).
- realDrop sorted ascending → assigned to positions with i ascending (index 1, 2, ..., n).

This setup ensures the difference ∑ i * (realDrop[i] - scheduledDrop[i]) is maximized.

---

**Step 4: Calculating the Result**

After sorting and rearranging arrays as per above:
- For each position i, calculate difference = realDrop[i] - scheduledDrop[i].
- Multiply difference by i (the position index).
- Sum all such terms for i from 1 to n.

Finally, take the result modulo (10^9 + 7) before returning.

---

**Step 5: Edge Cases and Validation**

- Arrays might contain very large values, so use 64-bit integers for intermediate computations to avoid overflow before modulo.
- Confirm that both arrays have the same length n.
- Handle modulo only at the final sum to prevent affecting intermediate computations unnecessarily.

---

**Summary**

To solve the problem:
- Sort scheduledDrop in ascending order.
- Sort realDrop in ascending order.
- Assign scheduledDrop smallest elements to highest i positions (reverse the sorted scheduledDrop).
- Assign realDrop in ascending order to i in ascending order.
- Compute the sum ∑ i * (realDrop[i] - scheduledDrop[i]) modulo 10^9+7.
- Return the computed sum.

This approach leverages the rearrangement inequality to pair elements optimally, ensuring the weighted drop delay is maximized.
```


Related Questions