Additional Supermarket Department Visits
Updated
Problem statement
Each row of products is [productName,departmentName]. shoppingList lists the products to buy.
Compare two ways to shop. In the first way, you buy the items in the listed order. A department visit starts at the first item and again each time the department changes. In the second way, you buy every item from one department together. That takes one visit for each different department you buy from. Return the number of visits in the first way minus the number in the second.
Each product has exactly one row in the catalog, and every listed product exists. Names are nonempty, case-sensitive strings. A product listed again is bought again at that spot in the list. It does not start a new visit if its department is the same as the item bought just before it. An empty shopping list returns zero. Departments you buy nothing from do not count.
Function
departmentSavings(products: String[][], shoppingList: String[]) → intExamples
Example 1
products = [["apple","fresh"],["banana","fresh"],["milk","dairy"]]shoppingList = ["apple","milk","banana"]return = 1In list order you go to fresh for apple, dairy for milk, then fresh again for banana. That is three visits. Grouped by department, you need only two visits: fresh and dairy. Three minus two saves one visit, so the answer is one.
Example 2
products = [["a","D"],["b","D"],["c","E"]]shoppingList = ["a","b","c"]return = 0The list already keeps each department together. In list order you visit D for a and b, then E for c. That is two visits, and grouping also takes two, so the answer is zero.
Constraints
0 <= products.length <= 100and0 <= shoppingList.length <= 300.- Every catalog row has two printable ASCII strings, each of length 1 through 20. Product names are unique, and every shopping item names a catalog product.
More Atlassian Phone Screen questions
- Merge Overlapping IntervalsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Tennis Court SchedulingPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Implement a Rate LimiterPHONE SCREEN · Seen Oct 2026
- Longest Consistent Activity StreakPHONE SCREEN · Seen Sep 2026
- Music Player Operation SequencePHONE SCREEN · Seen Sep 2026
- Underline Shortest Unique SubstringsPHONE SCREEN · Seen Aug 2026
- Can a Dictionary Word Be Formed from Note Letters?PHONE SCREEN · Seen Feb 2026
- Word Path Moving Only Right or DownPHONE SCREEN · Seen Feb 2026
- Ordered Badge Entry and Exit ViolationsPHONE SCREEN · Seen Aug 2025
- Two Sum with an Ordered Index PairPHONE SCREEN · Seen Aug 2025
Similar questions at other companies
- Transaction Fee Calculator — Parts 1–2: Basic Fee Calculation and Conditional Fee RulesStripe · PHONE SCREENStripe interview questions
- Detect a Straight FlushTemu · PHONE SCREENTemu interview questions
- First Letter to Appear TwiceCisco · PHONE SCREENCisco interview questions
- Halfway Course in a Single TrackCrunchyroll · PHONE SCREENCrunchyroll interview questions
- Single-Condition Product FilterCrowdstrike · PHONE SCREENCrowdstrike interview questions
- Summarize Server Healthcheck LogsNVIDIA · PHONE SCREENNVIDIA interview questions
- Known Words Matching Phone DigitsLinkedIn · PHONE SCREENLinkedIn interview questions
- First Non-Repeated CharacterDell Technologies · PHONE SCREENDell Technologies interview questions
- Convert an Entry List to a MapEPAM · PHONE SCREENEPAM interview questions
- First Non-Repeating CharacterGoldman Sachs · PHONE SCREEN · ONSITE INTERVIEWGoldman Sachs interview questions