Hone logo
Hone
Problems

Go Map Fundamentals: Building a Simple Inventory Tracker

This challenge will help you understand and practice the fundamental operations of Go's built-in map data structure. You will implement a basic inventory system where you can add, retrieve, and update item quantities. This is a common pattern for managing collections of data where each item has a unique identifier.

Problem Description

Your task is to create a Go program that simulates a simple inventory system using a map. The inventory will store items and their corresponding quantities. You need to implement functions to perform the following operations:

  • AddItem: Add a new item to the inventory or increase the quantity of an existing item.
  • GetQuantity: Retrieve the current quantity of a specific item. If the item doesn't exist, return 0.
  • RemoveItem: Remove an item from the inventory. If the item doesn't exist, do nothing.
  • ListItems: Return a slice of all item names currently in the inventory, sorted alphabetically.

Key Requirements:

  • Use a map[string]int to represent the inventory, where the string is the item name and the int is its quantity.
  • Ensure that AddItem correctly handles both adding new items and updating existing ones.
  • GetQuantity should return 0 for non-existent items, avoiding panics.
  • RemoveItem should be safe to call with non-existent items.
  • ListItems must return items in alphabetical order.

Expected Behavior:

The functions should modify and query the inventory map as described. The program should not panic or produce incorrect results when dealing with missing items.

Edge Cases:

  • Adding an item with a quantity of 0.
  • Retrieving the quantity of an item that has been removed.
  • Listing items when the inventory is empty.

Examples

Example 1: Basic Operations

Input:
inventory := make(map[string]int)
AddItem(inventory, "apple", 5)
AddItem(inventory, "banana", 10)
AddItem(inventory, "apple", 3) // Update existing
RemoveItem(inventory, "orange") // Item doesn't exist
RemoveItem(inventory, "banana")

Output:
GetQuantity(inventory, "apple") -> 8
GetQuantity(inventory, "banana") -> 0
GetQuantity(inventory, "orange") -> 0
ListItems(inventory) -> ["apple"]

Explanation:

  1. "apple" is added with quantity 5.
  2. "banana" is added with quantity 10.
  3. "apple" quantity is increased by 3, resulting in 8.
  4. "orange" is not in the inventory, so RemoveItem does nothing.
  5. "banana" is removed.
  6. GetQuantity for "apple" returns 8.
  7. GetQuantity for "banana" returns 0 (as it was removed).
  8. GetQuantity for "orange" returns 0 (as it was never added).
  9. ListItems returns the remaining item "apple" sorted alphabetically.

Example 2: Empty Inventory and Zero Quantity

Input:
inventory := make(map[string]int)
AddItem(inventory, "grape", 0)

Output:
GetQuantity(inventory, "grape") -> 0
ListItems(inventory) -> ["grape"]

Explanation:

  1. "grape" is added with quantity 0.
  2. GetQuantity for "grape" returns 0.
  3. ListItems returns "grape".

Example 3: Listing an Empty Inventory

Input:
inventory := make(map[string]int)

Output:
ListItems(inventory) -> []

Explanation: When the inventory is empty, ListItems returns an empty slice.

Constraints

  • Item names (string) will consist of lowercase English letters.
  • Quantities (int) will be non-negative.
  • The total number of distinct items in the inventory will not exceed 1000.
  • The total quantity of any single item will not exceed 10,000.
  • The ListItems function should have a time complexity of O(N log N) where N is the number of items, due to sorting. Other operations should aim for O(1) on average.

Notes

  • Remember that Go maps are unordered by default. You will need to use additional mechanisms to achieve alphabetical sorting for ListItems.
  • Consider how to check if an item exists in a map before attempting to update or remove it to avoid unexpected behavior.
  • The provided examples use a conceptual inventory variable to demonstrate the state changes. In your implementation, you will likely pass the map as an argument to your functions.
Loading editor...
go