Frontend School

Basket Item Count Validation

DSA
easy

Basket Item Count Validation

Easy
Arrays
Hashing

Overview

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.

What you need to do / Task

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:

  1. Count the occurrences of each product SKU in the inventory.
  2. Count the occurrences of each product SKU in the basket.
  3. Compare these counts: for every product in the basket, its requested quantity must be less than or equal to its available quantity in the inventory.
  4. Return true if all items in the basket can be fulfilled, and false otherwise.

Input & output

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.

Clarifications

  • Empty Arrays: If 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.
  • Case Sensitivity: Product SKUs should be treated as case-sensitive (e.g., "Apple" is different from "apple").
  • Data Types: All product SKUs are strings.

Examples

1

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.

2

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.

3

Example 3

Input:

inventory = ["mango", "grape"], basket = []

Output:

true

Note:

An empty basket always means no items are requested, thus it's fulfillable.

4

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

Constraints & assumptions

  • 0 <= inventory.length <= 10^5
  • 0 <= basket.length <= 10^5
  • 1 <= inventory[i].length, basket[i].length <= 50
  • Product SKUs consist of alphanumeric characters and hyphens.
  • The total number of unique SKUs across both inventory and basket will not exceed 10^5.

What interviewers evaluate

  • Correctness: Does the solution accurately identify if a basket is fulfillable under all specified conditions?
  • Efficiency: Is the solution optimal in terms of time and space complexity, particularly for large inputs?
  • Clarity and Readability: Is the code well-structured, easy to understand, and appropriately commented?
  • Edge Case Handling: Does the solution correctly handle cases like empty inventories, empty baskets, or items not present?

Loading editor...