Katkat

Determine Whether Package Weights Can Be Safely Arranged

AmazonSoftware Engineer · Technical Screen

Last updated

Tests array manipulation, counting techniques, and greedy or constraint-satisfaction logic to determine if an array can be permuted so no two adjacent elements sum to ten or more.

Problem Statement

At an Amazon fulfillment center, packages on a conveyor belt must be ordered so that the combined weight of any two adjacent packages does not exceed the sensor threshold of 9 (i.e., their sum must be strictly less than 10).

You are given a list of integers weights, where each package weight is between 0 and 9 inclusive. You may reorder the packages in any order.

Write a function can_safely_arrange(weights: list[int]) -> bool that returns True if there exists an ordering of weights such that for every adjacent pair (weights[i], weights[i + 1]), `weights[i] + weights[i + 1] bool: pass


## Examples

**Example 1:**
```text
Input: weights = [9, 0, 9, 1, 8]
Output: True
Explanation:
One valid arrangement is [9, 0, 9, 1, 8].
Adjacent sums:
- 9 + 0 = 9 < 10
- 0 + 9 = 9 < 10
- 9 + 1 = 10 (invalid!)
However, reordering to [9, 0, 8, 1, 9] yields:
- 9 + 0 = 9 < 10
- 0 + 8 = 8 < 10
- 8 + 1 = 9 < 10
- 1 + 9 = 10 (still invalid!)
Reordering to [8, 1, 0, 9, 0] is not possible because we only have one 0.
Let's check [9, 0, 8, 0, 9] - wait, we only have one 0 and one 1.
Pairs allowed for 9: only 0.
Pairs allowed for 8: 0, 1.
With two 9s, each needs 0 adjacent. One 0 can sit between two 9s: [9, 0, 9]. But 9 needs ends or more 0s.
If we place [9, 0, 9], both 9s are at ends or adjacent to 0. Then we have [8, 1]. We can form [9, 0, 8, 1, ... wait, 8 + 1 = 9 < 10, but 9 cannot touch 1 or 8].
Actually, [8, 1, 0, 9] leaves one 9. 9 must touch 0, but 0 can touch at most two items. Thus [9, 0, 9, ...] requires 0 to touch two 9s, leaving no way to connect the rest to 0. Hence this input is False.

Let's consider: weights = [9, 0, 8, 1, 0]
Output: True
Explanation: [9, 0, 8, 1, 0] gives sums 9, 8, 9, 1, all < 10.

Example 2:

Input: weights = [5, 5, 5, 5]
Output: False
Explanation:
Any two 5s sum to 5 + 5 = 10, which violates the condition (< 10). Because all packages are 5, at least two 5s must be adjacent, so no safe arrangement exists.

Constraints

  • 1 <= len(weights) <= 10^5
  • 0 <= weights[i] <= 9 for all i

Edge Cases to Consider

  • Arrays of length 1 are always valid since there are no adjacent pairs.
  • Arrays where all elements are = 5) can never be adjacent to one another because their sum is at least 5 + 5 = 10.

Hints

Hint 1

Notice that packages with weight 5 or greater can never be placed next to another package with weight 5 or greater, meaning they must be separated by packages strictly less than 5.

Hint 2

Each package of weight w >= 5 can only be adjacent to packages of weight at most 9 - w (e.g., 9 can only be placed next to 0; 8 can only be placed next to 0 or 1).

Hint 3

Track the frequency of each digit from 0 to 9, and verify whether a greedy left-to-right placement (always picking the valid element with the highest remaining demand or constraint) can complete the sequence.

Follow-up questions

  • How would you modify your solution to reconstruct and return the lexicographically smallest valid arrangement if one exists?
  • If the maximum allowable sum was a parameter K instead of fixed at 10, how does that affect your algorithm's complexity?
  • How would you adapt the algorithm to handle a real-time stream of incoming packages where you can maintain a small buffer of size B?