FastPrepPack Items
Problems/Pack Items

Pack Items

Amazon logoAmazon● EasyFULLTIMEOA

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

An Amazon fulfillment associate must pack items into two boxes, A and B. arr[i] is the weight of item i.

Split the items so that:

  • Every item goes into exactly one box.
  • Items with the same weight go into the same box.
  • The sum of the weights in A is greater than the sum in B.
  • A holds as few items as possible.

If several choices of A have the fewest items, pick the one with the largest sum. If there is still a tie, compare the choices from the heaviest item down. Pick the one with the heavier item at the first place where they differ.

Return the weights in A, sorted from smallest to largest.

For example, let arr = [4, 4, 1, 6]. The total is 15. [6] alone has 6, which is not more than 9. You cannot pick [4, 6], because the two 4s must stay in the same box. [4, 4] has 8, which is more than 7. The answer is [4, 4].

Function

packItems(arr: int[]) → int[]

Complete the function packItems in the editor.

packItems has the following parameter:

  • int arr[n]: the weight of each item

Returns int[]: the weights in box A, from smallest to largest

Examples

Example 1

arr = [3, 7, 5, 6, 2]return = [6, 7]

The total is 23, so A needs a sum of at least 12. No single item is enough. With 2 items, [5, 7] = 12 and [6, 7] = 13 both work. [6, 7] has the larger sum. The answer is [6, 7].

Example 2

arr = [4, 5, 2, 3, 1, 2]return = [4, 5]

The total is 17, so A needs a sum of at least 9. No single item is enough. With 2 items, only [4, 5] = 9 works. The answer is [4, 5].

Example 3

arr = [1, 2, 2, 2, 3, 4]return = [1, 3, 4]

The total is 14, so A needs a sum of at least 8. The three 2s must stay together. With 1 item, the most is 4. With 2 items, the most is 3 + 4 = 7, which is not enough. With 3 items, [1, 3, 4] = 8 works, but [2, 2, 2] = 6 does not. The answer is [1, 3, 4].

Example 4

arr = [2, 2, 2, 3]return = [2, 2, 2]

The three 2s must stay in the same box. [3] alone has 3, which is less than 6. [2, 2, 2] has 6, which is more than 3. The answer is [2, 2, 2].

Example 5

arr = [1,1,1,1,4,4,4,7,8]return = [4,4,4,8]

Equal weights stay together: four 1s, three 4s, one 7 and one 8. The total is 31, so A needs a sum of at least 16. [7, 8] has only 15, and the three 4s have only 12. With 4 items, [4, 4, 4, 7] = 19 and [4, 4, 4, 8] = 20 both work. [4, 4, 4, 8] has the larger sum. The answer is [4, 4, 4, 8].

Constraints

  • 1 <= n <= 1000, where n is the length of arr
  • 1 <= arr[i] <= 10^5

More Amazon interview questions

  • LRU Cache for Query ResultsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Maximal SquareONSITE INTERVIEW · Seen Nov 2026
  • Serialize and Deserialize a Binary TreeONSITE INTERVIEW · Seen Nov 2026
  • Course SchedulePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Course Schedule IIPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Diagonal Traversal of a Binary TreeONSITE INTERVIEW · Seen Nov 2026
  • Minimum Time to Spread Through a GridPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Consistent-Hash Packet RouterPHONE SCREEN · Seen Nov 2026
  • Count Connected ComponentsPHONE SCREEN · Seen Nov 2026
  • Maximize K-Element AND with an Increment BudgetOA · Seen Nov 2026

Similar questions at other companies

  • Catch Fish with Reusable BaitsSusquehanna International Group (SIG) · OASusquehanna International Group (SIG) interview questions
  • Maximum Apples That Fit in a BoxVirtu Financial · OAVirtu Financial interview questions
  • Server ManagementRippling · OARippling interview questions
  • Maximum Three-Server Difficulty ScoreVisa · OAVisa interview questions
  • Economical TripAccenture · OAAccenture interview questions
  • Minimum Pile Reduction StepsAmerican Express · OAAmerican Express interview questions
  • Maximum Production Within PowerZomato / Eternal · OAZomato / Eternal interview questions
  • Can Every Package Fit in a BoxIMC Trading · OAIMC Trading interview questions
  • Compute Walking DistanceTikTok · OATikTok interview questions
  • Minimum Tunnel Crossing TimeArcesium · OAArcesium interview questions

Interview reports that mention this question

  • Amazon Full-Time OA Problems, Earlier Reports (Part 5 of 5)

Amazon interview experiences

  • Amazon Experienced SDE Onsite Questions: 1 Experience
  • Amazon SDE Onsite Interview Questions: 9 Experiences
  • Amazon SDE II and L5 FDE Interview Questions: 4 Experiences
  • Amazon Full-Time OA Problems, Recent Reports (Part 4 of 5)
  • Amazon New Grad OA Problems: Every Task We Have (Part 3 of 5)
See Amazon hiring insights
Saved
class Solution {
    public int[] packItems(int[] arr) {
      // write your code here
    }

}
Checking account…