0
0
DSA Goprogramming~30 mins

BST vs Hash Map Trade-offs for Ordered Data in DSA Go - Build Both Approaches

Choose your learning style9 modes available
BST vs Hash Map Trade-offs for Ordered Data
📖 Scenario: Imagine you are building a simple contact list app. You want to store contacts with their phone numbers. Sometimes, you need to find a contact quickly by name. Other times, you want to list all contacts in alphabetical order.Two common ways to store this data are using a Binary Search Tree (BST) or a Hash Map. Each has its strengths and weaknesses.
🎯 Goal: Build a small program to store contacts using a map, then add a sorted list of contact names using a BST-like structure. This will help you see the difference in how data is stored and accessed.
📋 What You'll Learn
Create a map to store contact names and phone numbers.
Create a slice to hold contact names for sorting.
Sort the contact names alphabetically.
Print the contacts in alphabetical order.
💡 Why This Matters
🌍 Real World
Contact lists, phone books, and any app needing fast lookup and ordered display of data.
💼 Career
Understanding data structures like maps and trees helps in designing efficient software for search, sorting, and data management.
Progress0 / 4 steps
1
Create a map of contacts
Create a map called contacts with these exact entries: "Alice": "555-1234", "Bob": "555-5678", "Charlie": "555-8765"
DSA Go
Hint

Use map[string]string to create a map with string keys and string values.

2
Create a slice of contact names
Create a slice called names and add the keys from contacts to it using a for loop with variables name and phone iterating over contacts
DSA Go
Hint

Use for name, phone := range contacts to loop over the map and append to add names to the slice.

3
Sort the contact names alphabetically
Import the sort package and use sort.Strings(names) to sort the names slice alphabetically
DSA Go
Hint

Use import "sort" and then call sort.Strings(names) to sort the slice.

4
Print contacts in alphabetical order
Use a for loop with variable _, name iterating over names and print each contact's name and phone number from contacts using fmt.Println(name, contacts[name])
DSA Go
Hint

Use for _, name := range names and print with fmt.Println(name, contacts[name]).