Quick Overview

Decide whether package weights from zero through nine can be reordered so every adjacent sum stays below ten, including repeated values and separator constraints.

Determine Whether Package Weights Can Be Safely Arranged

Company: Amazon

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

## Problem Package weights are integers from 0 through 9. Determine whether the packages can be reordered so that the sum of every adjacent pair is strictly less than 10. Return only whether such an ordering exists. ### Function Contract Implement `can_arrange_weights(weights) -> bool`. ### Constraints - `0 <= len(weights) <= 200000`. - Every weight is an integer in `[0,9]`. - Each input occurrence must appear exactly once in the arrangement. - An empty or one-element array is valid. ### Examples - `[9,0,8,1,7]` returns `true`, for example via `9,0,8,1,7`. - `[9,9]` returns `false` because its only adjacent pair sums to 18. ```hint Exploit the tiny value domain A frequency array for the ten possible weights avoids comparison sorting and supports a state or greedy argument over counts. ``` ```hint Focus on incompatible adjacencies Large weights require sufficiently small neighbors; test any greedy placement rule against repeated 8s and 9s. ``` ### Edge Cases - Many copies of one weight may require separators. - Zero can neighbor every allowed weight. - Pairs summing to exactly 10 are invalid.

Quick Answer: Decide whether package weights from zero through nine can be reordered so every adjacent sum stays below ten, including repeated values and separator constraints.

Package weights are integers from 0 through 9. Return whether all input occurrences can be reordered so that the sum of every adjacent pair is strictly less than 10. Every occurrence must appear exactly once. Return only the boolean existence result; an empty or one-element array is valid.

Constraints

  • 0 <= len(weights) <= 200000.
  • Every weight is an integer in [0, 9].
  • Every input occurrence must appear exactly once in the arrangement.
  • Every adjacent sum must be strictly less than 10.
  • An empty or one-element array is valid.

Examples

Input: ([9, 0, 8, 1, 7],)

Expected Output: True

Explanation: One valid order is 9, 0, 8, 1, 7.

Input: ([9, 9],)

Expected Output: False

Explanation: The only adjacent pair would sum to 18.

Hints

  1. Use a frequency array for the ten possible values.
  2. Weights with fewer compatible neighbors are the critical ones in the threshold compatibility graph.

Loading coding console...