FastPrepAdditional Supermarket Department Visits

Additional Supermarket Department Visits

Atlassian logoAtlassian● EasyFULLTIMEPHONE SCREEN
Learn

Problem statement

Each row of products is [productName,departmentName], and shoppingList gives the products to buy.

Compare two ways to shop. Buying products in the listed order starts a department visit at the first item and whenever the department changes. Buying every item from one department together requires one visit per distinct purchased department. Return the first visit count minus the second.

For this exercise, assume each product has exactly one catalog row, every listed product exists, and names are nonempty case-sensitive strings. A repeated product is another purchase in that position; it does not create a new visit when its department matches the previous purchase. An empty shopping list returns zero. Unpurchased departments do not count.

Function

departmentSavings(products: String[][], shoppingList: String[]) → int

Examples

Example 1

products = [["apple","fresh"],["banana","fresh"],["milk","dairy"]]shoppingList = ["apple","milk","banana"]return = 1

The source visits fresh, dairy, fresh: three visits versus two, saving one.

Example 2

products = [["a","D"],["b","D"],["c","E"]]shoppingList = ["a","b","c"]return = 0

Already grouped shopping makes three purchases in two department visits, so savings is zero.

Constraints

  • For this exercise, assume 0 <= products.length <= 100 and 0 <= shoppingList.length <= 300.
  • For this exercise, assume every catalog row has two printable ASCII strings of length 1 through 20, product names are unique, and every shopping item names a catalog product.

More Atlassian problems

See Atlassian hiring insights
public int departmentSavings(String[][] products, String[] shoppingList) {
    // Write your code here.
    return 0;
}
products[["apple","fresh"],["banana","fresh"],["milk","dairy"]]
shoppingList["apple","milk","banana"]
expected1
Checking account…