AMAZON Coding Question – Solved

12 Live
The engineering team at an Amazon fulfillment center is optimizing n high-performance printers, where each printer can print pages[i] number of pages. Each printer can be in exactly one of three states: operational, idle, or suspended. Printers initially start in an idle state and can be activated one by one. However, if too many printers are active at once, some will get suspended due to their threshold limit defined by the suspension rule below. Suspension Rule: If there are at least x operational printers, all such printers with threshold[i] ≤ x will get suspended and stop printing. Task: The goal is to determine the maximum number of pages that can be printed before printers get suspended. Note: Activating a printer with threshold[i] = x allows it to print pages[i] pages. However, once at least x printers are active, their pages get printed first, and then all the printers with threshold ≤ x will get suspended immediately. Choosing the activation order carefully is therefore crucial to maximize the total printed pages before suspensions occur. Example: n = 4 pages = [2, 6, 10, 13] threshold = [2, 1, 1, 1] The optimal way to maximize the number of pages printed is as follows (using 1-based indexing): First, the engineers decide to activate the 4th printer, which prints 13 pages. At this point, the total number of operational printers is 1. The printing of 13 pages is completed first, followed by the suspension of any printers exceeding their threshold. Next, since the threshold for printers 2, 3, and 4 is 1, and there is now 1 operational printer (4th printer), these printers become damaged. Thus, all the printers (2nd, 3rd, and 4th) with threshold = 1 get suspended and stop functioning. Next, the only printer the team can turn on is printer 1. By activating printer 1, they print 2 more pages. The number of operational printers is now 1, and because threshold[1] = 2, printer 1 will not be suspended and remains functional. Thus, the total number of pages printed is 13 (from printer 4) + 2 (from printer 1) = 15. Hence, the answer is 15. Function Description Complete the function getPages in the editor below. getPages has the following parameter(s): int pages[n]: number of pages that printers can print int threshold[n]: threshold values of printers

Asked in: AMAZON

Image of the Question

Question Image Question Image Question Image

All Testcases Passed ✔



Passcode Image

Solution


from collections import defaultdict
def getPages(pages, threshold):
    # Write your code here
    h = defaultdict(list)
// ... rest of solution available after purchase

🔒 Please login to view the solution

Explanation


```
To solve this problem, you need to deeply understand the interplay between the number of operational printers and their thresholds, and how that affects when printers get suspended. The core challenge is to activate printers in an order that maximizes the total pages printed before suspension rules force printers offline.

Let’s break down the problem and outline the reasoning and steps to tackle it:

1. **Problem Restatement and Key Observations:**
You have n printers, each with two attributes: how many pages it prints (pages[i]) and a threshold (threshold[i]) that determines when it will be suspended.
Initially, all printers are idle. You activate printers one by one. Once activated, a printer immediately prints its pages.
The critical rule is that once the count of active printers reaches or exceeds x, all printers with threshold ≤ x are suspended instantly (after printing their pages if they were just activated). This means that the suspension condition depends on the current number of active printers, and printers with low thresholds get suspended if too many printers are active.

2. **What does suspension mean practically?**
Once a printer is suspended, it stops printing and cannot be reactivated. This directly means you lose the potential pages from those printers if they are suspended early.

3. **Goal: Maximize total printed pages before printers get suspended**
Since suspension happens immediately after the print phase whenever the active count reaches or surpasses some value, your strategy is about *choosing the activation order* to maximize pages before suspension.

4. **Understanding the suspension trigger:**
Suppose you have x active printers. Then all printers with threshold ≤ x get suspended (including those just activated). This implies:
- If you activate too many printers quickly, you may suspend a lot of printers early.
- Activating printers with low thresholds early is risky because as soon as you have x active printers with x ≥ their threshold, they all get suspended.
- Activating printers with higher thresholds first might allow you to accumulate more pages before suspensions kick in.

5. **Analyzing the example:**
In the example, activating the printer with the largest pages first and with a low threshold (like printer 4, threshold=1) lets you print a large chunk before suspensions occur. But after that, many printers get suspended, leaving fewer printers active but still allowing some printers with higher thresholds to be activated safely.

6. **Key insight: Sort printers by thresholds and pages:**
The problem hinges on the threshold values and how many printers you activate. Intuitively:
- Printers with low thresholds get suspended sooner because they tolerate fewer active printers.
- Printers with high thresholds can tolerate more active printers.
- You want to prioritize printers that produce more pages and survive longer.

7. **Two potential approaches to explore:**
- **Greedy by pages:** Try activating printers in descending order of pages. This ensures you print as many pages as early as possible. But this can cause early suspensions if low-threshold printers get activated first or are counted.
- **Greedy by threshold:** Activate printers with the highest thresholds first, because they can remain operational longer as more printers come online.

8. **Combining insights:**
You want a strategy that balances both pages and threshold:
- You want to activate printers with high pages and high threshold first to maximize pages and reduce suspension risk.
- You might want to defer activating low threshold printers until you have fewer active printers or none at all.

9. **Tracking active printers and suspension:**
To implement this reasoning, think about this:
- Each time you activate a printer, increment the count of active printers.
- Immediately after activation, suspend printers with threshold ≤ current active count. This suspension reduces active printer count.
- You must carefully choose the order so that suspension happens at times that minimize page loss.

10. **Data structure considerations:**
To manage suspensions, consider using a data structure (like a priority queue or balanced tree) to track printers by threshold or pages dynamically, enabling you to quickly decide which printers to activate next.

11. **Strategy Outline:**
- Sort printers in descending order by pages or by some combined metric considering threshold and pages.
- Activate printers one by one in this order.
- After each activation, update the count of active printers.
- Suspend all printers whose threshold ≤ active count. This might mean removing several printers from your active set, which affects future activations.
- Continue until no printers remain to activate or suspension removes all remaining printers.

12. **Optimization points:**
Since every activation changes the count and suspensions, consider the impact of each printer’s threshold relative to the active count dynamically.
A key challenge is predicting which printer activation will cause minimal or no suspensions and yield maximum pages.

13. **Final result:**
Sum the pages of all printers that get activated and print before suspension removes them. Your final answer is this total sum.

14. **Summary:**
Think about the problem as a scheduling/ordering puzzle where the constraints depend on the count of active tasks (printers) and their thresholds. You want to maximize the output of printers that remain active longest, activating high-page printers with large thresholds first, avoiding activating too many low-threshold printers early. Use sorting and a dynamic process to track and update active printers and suspensions, ensuring you pick an order that yields the maximum printed pages before suspensions.

In essence, the solution revolves around careful ordering, real-time suspension handling based on active counts, and maximizing the cumulative printed pages before printers get suspended.
```


Related Questions