FastPrepMinimum Baskets with Distinct Fruit Types

Minimum Baskets with Distinct Fruit Types

Amazon logoAmazon● MediumFULLTIMEONSITE INTERVIEW
Learn

Problem statement

Place every input fruit into a basket using the fewest possible baskets. Each basket may contain at most one fruit of each type.

For this exercise, assume the output is deterministic: scan fruits in input order and place the first occurrence of each type in basket 0, the second in basket 1, and so on. Within a basket, retain input order. Every fruit must occur exactly once in the result. Empty input returns an empty list.

Function

minimumFruitBaskets(fruits: String[]) → String[][]

Examples

Example 1

fruits = ["a","b","c","a","b","c"]return = [["a","b","c"],["a","b","c"]]

Two copies of each type require at least two baskets.

Example 2

fruits = ["pear","apple","pear"]return = [["pear","apple"],["pear"]]

The second pear cannot share a basket with the first.

Example 3

fruits = []return = []

No baskets are required.

Constraints

  • For this exercise, assume 0 <= fruits.length <= 2000.
  • Every type is a nonempty lowercase English-letter string of length at most 20.

More Amazon problems

See Amazon hiring insights
public String[][] minimumFruitBaskets(String[] fruits) {
    // Write your code here
}
fruits["a","b","c","a","b","c"]
expected[["a", "b", "c"], ["a", "b", "c"]]
Checking account…