Amazon
  • Introduction
  • Phone Interview I
    • 53.Reverse Words in a String
    • 31.Partition Array
  • Phone Interview II
    • 167.Add Two Numbers
    • 88.Lowest Common Ancestor
  • Onsite I
    • 655.Big Integer Addition
    • 221.Add Two Numbers II
  • Onsite II
    • 158.Two Strings Are Anagrams
    • 171.Anagrams
    • 386.Longest Substring with At Most K Distinct Characters
  • Onsite III
    • 479.Second Max of Array
    • 589.Connecting Graph
  • Onsite IV
    • 532.Reverse Pairs
  • 2022
    • OA
      • work simulation
      • Greyness
      • NearestRetailer
      • Sum of Scores of Subarray
      • StrengthOfPassword
      • ProductOf1
      • Move 0/1 InArray
      • Max deviation among all substrings
      • AWS power consumption
      • searchWordResultWord
      • maxOperationOfString
      • MinHealthGame
      • EarliestMonth
      • Package ship
      • RatingConsectiveDecresing
      • LinkedListSum
      • MovingBoxes
      • ValidString
      • MaxValueAfterRemovingFromString
      • Subtree with Maximum Average
    • VO
      • 2022.3
    • BQ
      • doc
      • 2022.4
      • Freq Question
      • 11大类BQ和Follow-ups
      • Page 1
      • BQ 100
      • 35 behavioral questions asked in 95% of Amazon interviews with examples
      • Page 2
      • 反向BQ
    • LP
      • LP-1
      • LP-2
    • SD
      • Design Amazon Prime Video Recommendation System
      • Amazon Order system
    • OOD
      • Linux Find Command
      • Amazon Locker
    • AWS Identity call
    • Interviews
Powered by GitBook
On this page

Was this helpful?

  1. 2022
  2. OA

MovingBoxes

https://leetcode.com/discuss/interview-question/1733741/Amazon-OA-or-SDE-Intern

You are an amazon delivery and you have some boxes that you have to deliver, but there are some conditions -

  • You can take 2 boxes of same weight in one round

  • you can take 3 boxes of same weight in one round

You have to find the minimum number of rounds to deliver the boxes or -1 if it is not possible to deliver them.

Example cases - Input: boxes - [2, 2, 3, 3, 2, 4, 4, 4, 4, 4] Output: 4 Explanation: 3 boxes of weight 2 in 1st round, 2 boxes of weight 3 in 2nd round, 3 boxes of wt 4 in 3rd and 2 boxes of wt 4 in 4th round.

Input: boxes - [2, 3, 3] Output: -1 Explanation: There is only one box with weight 2 and we can only take either 2 or 3 boxes in one round not lesser.

My Approach:

  • Make a Counter of frequency of weights

  • If there is any weight with frequency 1 return -1

  • otherwise iterate through all frequencies and calculate minimum number of rounds for that frequency.

  • this is similar to minimum number of coins to change the particular value where coins are 2 and 3 Let f is the maximum of frequency of any weight and there are c unique weights then Time Complexity: O(max(f, c)) Space Complexity: O(f)

  • Minimum number of rounds can be calculated directly in O(1) because we can convert all the numbers as a sum of 2 and 3. (this came to my mind after submitting the test, I solved this using coin change method using DP)

PreviousLinkedListSumNextValidString

Last updated 3 years ago

Was this helpful?