EXPEDIA Coding Question – Solved

11 Live
Given two servers and the time taken to upgrade each server in seconds, denoted by t1 and t2 respectively, in one second, one server undergoes the upgrade process. The servers receive requests at certain time intervals and pause upgrades during those seconds. The servers receive requests at multiples of req1 and req2 respectively. Determine the minimum total time (in seconds) required to upgrade both servers. Notes: . Only one server undergoes the upgrade process at any given second. . There may be seconds during which no server is undergoing an upgrade. Example: req1 = 2 t1 = 3 req2 = 3 t2 = 1 The 1st server takes 3 seconds to upgrade, and it receives requests on seconds that are multiples of 2. Similarly, the 2nd server upgrades in 1 second and receives requests on seconds that are multiples of 3. The 1st server upgrades in the 1st, 3rd, and 5th seconds, while the 2nd server upgrades in the 2nd second. Note that none of the numbers from [1, 3, 5] is divisible by req1 = 2. Similarly, [2] is not divisible by req2 = 3. Thus, the minimum time required is 5 seconds. Function Description: Complete the function getMinUpgradationTime in the editor below. getMinUpgradationTime takes the following parameter(s): int req1: indicates that the first server receives requests at multiples of req1 int t1: the total time in seconds to upgrade the first server int req2: indicates that the second server receives requests at multiples of req2 int t2: the total time in seconds to upgrade the second server Returns: long: the minimum total time (in seconds) required to upgrade both servers Constraints: · 2 ≤ req1, req2 ≤ 3 * 10^4 · 1 ≤ t1, t2 ≤ 10^9 Sample Input: 2 1 FUNCTION req1 = 2 t1 = 1 Input Format For Custom Testing: Sample Case 0 Sample Input For Custom Testing: STDIN

Asked in: EXPEDIA

Image of the Question

Question Image Question Image Question Image

All Testcases Passed ✔



Passcode Image

Solution


import math
def lcm(x,y):
    return abs(x*y)//math.gcd(x,y)

// ... rest of solution available after purchase

🔒 Please login to view the solution

Explanation


```
To approach this problem, first thoroughly understand the constraints and conditions:

You have two servers to upgrade, each requiring a certain number of seconds (t1 and t2 respectively). Upgrades happen one second at a time, and only one server can be upgraded in any given second (no parallel upgrades). Additionally, each server receives requests at specific intervals (multiples of req1 for server 1, multiples of req2 for server 2). During the seconds when a server receives a request, that server cannot be upgraded. This means the upgrade process for that server must pause or skip those seconds.

The goal is to determine the minimal total elapsed time needed to complete upgrading both servers, respecting the constraints of no overlapping upgrades, pauses during request times, and upgrading only one server at any second.

---

### Step 1: Understand the timing restrictions for upgrades and requests

- The servers receive requests on specific seconds: all multiples of req1 for server 1, and all multiples of req2 for server 2.
- If a second is a multiple of req1, server 1 cannot be upgraded in that second (the server is busy handling a request).
- Similarly, if a second is a multiple of req2, server 2 cannot be upgraded in that second.
- If a second is a multiple of both req1 and req2, neither server can be upgraded in that second.

Effectively, these seconds are "blocked" for upgrading the corresponding server.

---

### Step 2: Upgrade scheduling constraints

- Only one server can be upgraded at any given second. This means no concurrent upgrades.
- If both servers are ready to upgrade, only one can be chosen at a time.
- There can be seconds when no upgrades happen because the second is blocked for both servers due to requests.
- The upgrade does not need to be continuous for each server; the upgrade seconds for a server can be spread out, interleaved with the other server’s upgrade seconds or idle seconds.

---

### Step 3: What does the minimal total time mean?

The minimal total time is the earliest second after which both servers have completed all their required upgrade seconds, considering the blocked request times and the rule of upgrading one server at a time.

---

### Step 4: Modeling the problem

Think of this as a timeline starting from second 1, incrementing by one each time.

For each second:

- Check if server 1 can be upgraded (the second is not a multiple of req1).
- Check if server 2 can be upgraded (the second is not a multiple of req2).

From the seconds that are available for each server (not request-blocked), assign upgrade seconds to servers in such a way that:

- Neither server gets upgrade seconds at times when it is blocked.
- Only one server gets upgraded per second.
- Both servers get exactly t1 and t2 upgrade seconds respectively.

The goal is to find the minimal total time T such that it is possible to assign upgrade seconds to servers within the interval [1, T].

---

### Step 5: Binary search for the minimal time T

Because the upgrade seconds for both servers are constrained by the request times, and upgrades cannot overlap, the minimal total time T is bounded below by the sum of t1 and t2 (if no request conflicts) and above by some large number (e.g., a function of t1, t2, req1, req2).

To efficiently find this minimal T, consider applying a binary search over T:

- Set low to 1, high to a large number (for example, max(t1, t2) * max(req1, req2) or some upper bound).
- For a candidate time mid = (low + high) // 2, check if it is possible to schedule all upgrade seconds for both servers within [1, mid].

---

### Step 6: How to check feasibility for a given time T?

At time T, determine:

- How many seconds in [1, T] are available for upgrading server 1? This is the count of seconds in [1, T] that are **not multiples** of req1.
- Similarly, how many seconds in [1, T] are available for upgrading server 2? Count of seconds in [1, T] that are **not multiples** of req2.
- Since only one upgrade can happen per second, the total number of seconds available for upgrading either server (excluding seconds where both servers are blocked) is T minus the count of seconds that are multiples of both req1 and req2.

The critical aspect here is the overlapping blocked times—seconds which are multiples of both req1 and req2. These seconds are blocked for both servers simultaneously and cannot be used to upgrade either server.

From these counts:

- Verify if the available upgrade seconds for server 1 is at least t1.
- Verify if the available upgrade seconds for server 2 is at least t2.
- Verify if total available upgrade seconds (those not blocked for both servers) is at least t1 + t2.

If these conditions hold, then it is possible to schedule the upgrades within time T.

---

### Step 7: Computing counts of available seconds

- Number of seconds blocked for server 1: floor(T / req1)
- Number of seconds blocked for server 2: floor(T / req2)
- Number of seconds blocked for both servers: floor(T / L), where L is the Least Common Multiple (LCM) of req1 and req2.

Then:

- Seconds available for server 1 upgrades: T - floor(T / req1)
- Seconds available for server 2 upgrades: T - floor(T / req2)
- Total seconds available for upgrading (excluding double-blocked seconds): T - floor(T / req1) - floor(T / req2) + floor(T / L) (using inclusion-exclusion principle)

Use these calculations to check feasibility as described.

---

### Step 8: Iteratively refine the search range

Use binary search to narrow down the minimum T:

- If the current T is feasible (can schedule upgrades), try smaller values to see if you can do better.
- If not feasible, increase T.
- Repeat until low and high converge to the minimal feasible time.

---

### Step 9: Final thoughts and edge cases

- When req1 equals req2, LCM is req1 = req2.
- When t1 or t2 is zero, the problem reduces to upgrading one server only.
- Handle large inputs efficiently without enumerating seconds.
- Be careful with integer division and potential off-by-one errors.

---

### Summary:

- Model upgrade availability as a function of T, based on multiples of req1 and req2.
- Use binary search on T to find the minimum time for feasible scheduling.
- Calculate available upgrade seconds using floor division and LCM.
- Verify feasibility conditions at each step of binary search.
- Return the minimal feasible time.

This approach efficiently handles large input constraints and logically handles request-blocked seconds and exclusive upgrade scheduling.
```


Related Questions