AMAZON Coding Question – Solved

5 Live
Amazon is working on optimizing its delivery zones to ensure efficient and timely delivery of packages. Each delivery zone is represented as an interval indicating the range of house numbers it covers. The delivery zones can overlap. Given is a list of n delivery zones, where the i-th zone covers the interval (a[i], b[i]) (inclusive). Additionally, given is a maximum allowed length, k, for any new delivery zone that can be added. Your task is to add exactly one new delivery zone (a, b) such that the length of this new zone (b - a) is less than or equal to k. The goal is to minimize the number of disconnected delivery zones after adding the new delivery zone. A set of delivery zones [ (a[1], b[1]), (a[2], b[2]), ..., (a[n], b[n])] is considered connected if every house number in the range (min(a[1], a[2], ..., a[n]), max(b[1], b[2], ..., b[n])) is covered by at least one of the delivery zones (a[i], b[i]) in the set. For an instance: - The set [(1, 2), (2, 3), (1, 5)] is connected because every house number in the interval (min(1, 2, 1), max(2, 3, 5)) = (1, 5) is covered by at least one of the delivery zones. - The set [(2, 2), (3, 4)] is not connected because the delivery zones (2, 2) and (3, 4) do not overlap each other and hence is disconnected. Note: The arrays 'a' and 'b' used above are considered to follow 1-based indexing. Example Consider the delivery zones: [(1, 5), (2, 4), (6, 6), (7, 14), (16, 19)] and k = 2. If you add a new delivery zone (5, 7) to the list, you can separate the zones into 2 connected sets: - [(1, 5), (2, 4), (5, 7), (6, 6), (7, 14)] - [(16, 19)] However, if you add a new delivery zone (14, 16), you will end up with 3 connected sets: - [(1, 5), (2, 4)] - [(6, 6)] - [(14, 16), (16, 19)]

Asked in: AMAZON

Image of the Question

Question Image Question Image

All Testcases Passed ✔



Passcode Image

Solution


def minimumSets(a, b, k):
    # Write your code here
    array = [(left,right) for left,right in zip(a,b)]
    array.sort()
// ... rest of solution available after purchase

🔒 Please login to view the solution

Explanation


```
To approach this problem, start by understanding the core concept of what it means for delivery zones to be "connected." A set of intervals (delivery zones) is connected if there are no gaps between them when you consider their union from the minimum starting point to the maximum ending point. That means every house number in this range is covered by at least one delivery zone, so there are no uncovered gaps.

You are given existing delivery zones that may overlap or be disjoint, and you want to add exactly one new delivery zone of length at most k. The goal is to minimize the number of disconnected groups of intervals after adding this new zone.

First, visualize or imagine the existing intervals laid out on a number line. Some intervals may overlap or touch, forming connected groups, while others are separated by gaps, forming disconnected groups. The number of these disconnected groups can be counted by merging intervals and counting how many separate merged intervals result.

Your task is to insert a new interval, with length ≤ k, anywhere on the number line, to reduce these disconnected groups as much as possible. Since adding a new interval can only connect intervals that are separated by gaps smaller than or equal to the new interval’s length, the new interval ideally should be placed in a gap between two existing connected components to join them.

### Step 1: Merge existing intervals to find connected components
Begin by sorting the given intervals by their starting point. Then merge overlapping or contiguous intervals to find the current connected groups. For example, if intervals overlap or touch, combine them into one merged interval. After this, you will have a list of disjoint merged intervals representing current connected sets.

### Step 2: Identify gaps between merged intervals
Look at the gaps between these merged intervals. Each gap is defined as the difference between the end of one merged interval and the start of the next merged interval, minus 1 (if intervals are inclusive). These gaps represent the spaces where no delivery zones currently exist.

### Step 3: Analyze the feasibility of bridging gaps
The only way to reduce the number of disconnected groups is to add a new delivery zone that spans at least part of one or more of these gaps. Since the new interval length cannot exceed k, you can only bridge gaps of length ≤ k.

### Step 4: Explore how to place the new interval
For each gap between merged intervals:
- Calculate the size of the gap.
- If the gap size is larger than k, you cannot bridge it directly with the new interval.
- If it is smaller or equal to k, placing the new interval to cover that gap will merge the two previously disconnected connected groups into one, reducing the total count by 1.

### Step 5: Consider merging multiple gaps if possible
If the new interval length k is large enough, it may be possible to bridge multiple adjacent gaps at once, joining more than two connected groups simultaneously. For example, if there are three connected groups separated by two gaps, and the total combined gap length between the first and third group is less than or equal to k, placing the new interval there merges all three into a single group.

To find this:
- Consider intervals of gaps in the sorted list of merged intervals.
- Use a sliding window approach or prefix sums to find the maximum number of consecutive gaps you can bridge with an interval length ≤ k.

### Step 6: Choose the optimal placement of the new interval
Among all possible positions for the new interval (covering a single gap or multiple consecutive gaps), select the placement that yields the greatest reduction in the number of disconnected groups.

If no gaps are small enough to be bridged (i.e., all gaps are larger than k), then the best you can do is place the new interval anywhere, which will not reduce the number of disconnected groups.

### Step 7: Calculate the final answer
The minimum number of disconnected groups after adding the new interval will be:
- Original number of connected groups minus the maximum number of groups you can merge by bridging gaps with the new interval.

### Important considerations
- The new interval length is flexible up to k; it can be shorter.
- You are allowed to place the new interval anywhere, so you don’t have to align it exactly with existing intervals. Just ensure it fully covers the gaps you want to bridge.
- Intervals can be inclusive, so consider carefully whether touching intervals are connected or not.
- Edge cases: If intervals are already fully connected, adding a new interval does not change the count.
- Handle intervals that are points (start == end).
- Be careful when dealing with intervals at the extreme ends of the number line.

### Summary
To summarize, the key steps are:
- Merge the given intervals to identify current connected groups.
- Identify gaps between these groups.
- Evaluate which gaps or sequences of gaps can be covered by a new interval of length ≤ k to connect groups.
- Select the optimal gap or combination of gaps to cover to minimize disconnected groups.
- The final answer is the original number of connected groups minus the maximum number of groups merged through this new interval.

This approach leverages interval merging, gap analysis, and interval coverage optimization to solve the problem efficiently without brute forcing every possible interval placement.
```


Related Questions