Basket Item Count Validation
At Bigbasket, ensuring customers receive their desired products accurately and promptly is paramount. Before processing an order, our system needs to quickly verify if all items in a customer's virtual shopping basket are available in the warehouse inventory with sufficient quantities. This problem challenges you to implement a core function for this critical validation step.
Implement a function checkBasketAvailability that determines whether all products in a given customer's basket can be fulfilled based on the current inventory.
Specifically, your function should:
inventory.basket.basket, its requested quantity must be less than or equal to its available quantity in the inventory.true if all items in the basket can be fulfilled, and false otherwise.Function Signature:
function checkBasketAvailability(inventory: string[], basket: string[]): boolean
Parameters:
inventory: An array of strings, where each string represents a unique product SKU currently available in the warehouse. Duplicates indicate multiple units of the same product.basket: An array of strings, where each string represents a product SKU a customer wants to purchase. Duplicates indicate multiple units of the same product requested.Return Value:
boolean: true if all items in the basket can be fulfilled from the inventory with sufficient quantities; false otherwise.basket is empty, it means no items are requested, so it should always be considered fulfillable (return true). If inventory is empty but basket is not, it means nothing is available, so it should return false.Examples
Example 1
Input:
inventory = ["apple", "banana", "apple", "orange"], basket = ["apple", "banana"]
Output:
true
Note:
Inventory has 2 apples, 1 banana, 1 orange. Basket needs 1 apple, 1 banana. All items can be fulfilled.
Example 2
Input:
inventory = ["milk", "bread", "milk"], basket = ["milk", "milk", "milk", "eggs"]
Output:
false
Note:
Inventory has 2 milks, 1 bread. Basket needs 3 milks, 1 egg. Not enough milk and eggs are missing entirely.
Example 3
Input:
inventory = ["mango", "grape"], basket = []
Output:
true
Note:
An empty basket always means no items are requested, thus it's fulfillable.
Example 4
Input:
inventory = ["rice", "dal"], basket = ["rice", "sugar"]
Output:
false
Note:
Inventory has rice and dal. Basket needs rice and sugar. Sugar is not available.
Constraints
0 <= inventory.length <= 10^50 <= basket.length <= 10^51 <= inventory[i].length, basket[i].length <= 50inventory and basket will not exceed 10^5.Loading editor...