0
0
DSA Typescriptprogramming~30 mins

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

Choose your learning style9 modes available
BST vs Hash Map Trade-offs for Ordered Data
📖 Scenario: Imagine you are managing a small library system. You want to store book titles and their number of copies. You want to quickly find how many copies a book has, but also sometimes list books in alphabetical order.
🎯 Goal: You will create a simple data structure using a JavaScript object (hash map) and a sorted array (to simulate BST behavior) to understand the trade-offs between fast lookup and ordered data.
📋 What You'll Learn
Create a hash map object called bookCopies with given book titles and copies
Create a sorted array called sortedBooks containing the book titles in alphabetical order
Write a function getCopiesFromMap that takes a book title and returns copies from the hash map
Write a function listBooksInOrder that returns the sorted list of book titles
Print the number of copies for a specific book using getCopiesFromMap
Print the list of books in alphabetical order using listBooksInOrder
💡 Why This Matters
🌍 Real World
Libraries, inventory systems, and many apps need to store data for quick lookup and sometimes in order for reports or display.
💼 Career
Understanding these trade-offs helps software developers choose the right data structure for performance and functionality.
Progress0 / 4 steps
1
Create the hash map of books and copies
Create a variable called bookCopies as an object with these exact entries: 'The Hobbit': 5, '1984': 8, 'Brave New World': 3, 'Fahrenheit 451': 6
DSA Typescript
Hint

Use curly braces {} to create an object and add key-value pairs for each book and its copies.

2
Create a sorted array of book titles
Create a variable called sortedBooks as an array containing the book titles from bookCopies sorted alphabetically
DSA Typescript
Hint

Use Object.keys(bookCopies) to get the titles, then .sort() to order them alphabetically.

3
Write functions for lookup and ordered listing
Write a function called getCopiesFromMap that takes a parameter title and returns the number of copies from bookCopies. Also write a function called listBooksInOrder that returns the sortedBooks array
DSA Typescript
Hint

Use simple functions that access the object and return the sorted array.

4
Print copies and ordered book list
Print the number of copies for the book '1984' using getCopiesFromMap('1984'). Then print the list of books in alphabetical order using listBooksInOrder()
DSA Typescript
Hint

Use console.log to print the results of the function calls.