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]intto represent the inventory, where thestringis the item name and theintis its quantity. - Ensure that
AddItemcorrectly handles both adding new items and updating existing ones. GetQuantityshould return 0 for non-existent items, avoiding panics.RemoveItemshould be safe to call with non-existent items.ListItemsmust 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:
- "apple" is added with quantity 5.
- "banana" is added with quantity 10.
- "apple" quantity is increased by 3, resulting in 8.
- "orange" is not in the inventory, so
RemoveItemdoes nothing. - "banana" is removed.
GetQuantityfor "apple" returns 8.GetQuantityfor "banana" returns 0 (as it was removed).GetQuantityfor "orange" returns 0 (as it was never added).ListItemsreturns 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:
- "grape" is added with quantity 0.
GetQuantityfor "grape" returns 0.ListItemsreturns "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
ListItemsfunction 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
inventoryvariable to demonstrate the state changes. In your implementation, you will likely pass the map as an argument to your functions.