📖 Scenario: Imagine you are managing a small library's book collection. You want to store book titles and their number of copies. You need to quickly find how many copies a book has, but sometimes you also want to see all books sorted by title.
🎯 Goal: You will create a simple data structure to store books and their copies. Then, you will add a way to get all books sorted by title. This will help you understand when to use a Binary Search Tree (BST) or a Hash Map for storing ordered data.
📋 What You'll Learn
Create a JavaScript object called bookCopies with exact book titles as keys and their copies as values.
Create a variable called sortedTitles to hold the sorted list of book titles.
Use a method to get all book titles from bookCopies and sort them alphabetically.
Print the sorted list of book titles.
💡 Why This Matters
🌍 Real World
Libraries, stores, and many apps need to store items with quick lookup and sometimes sorted order for display.
💼 Career
Understanding when to use hash maps or trees helps in building efficient software for searching and sorting data.
Progress0 / 4 steps
1
Create the bookCopies object
Create a JavaScript object called bookCopies with these exact entries: 'The Hobbit': 5, '1984': 8, 'Brave New World': 3, 'Fahrenheit 451': 6, 'Animal Farm': 4.
DSA Javascript
Hint
Use curly braces {} to create an object. Each book title is a key in quotes, followed by a colon and the number of copies.
2
Create sortedTitles variable
Create a variable called sortedTitles and set it to an empty array [].
DSA Javascript
Hint
Use let to create a variable that can change later. Set it to an empty array [].
3
Get and sort book titles
Set sortedTitles to the list of keys from bookCopies sorted alphabetically using Object.keys(bookCopies).sort().
DSA Javascript
Hint
Use Object.keys() to get all keys from the object, then use .sort() to sort them alphabetically.
4
Print the sorted book titles
Print the sortedTitles array using console.log(sortedTitles).
DSA Javascript
Hint
Use console.log() to print the array to the console.
Practice
(1/5)
1. Which data structure is best suited when you need to maintain elements in a sorted order and perform range queries efficiently?
easy
A. Binary Search Tree (BST)
B. Hash Map
C. Stack
D. Queue
Solution
Step 1: Understand the need for sorted order
Maintaining elements in sorted order requires a structure that keeps data sorted as elements are added or removed.
Step 2: Compare BST and Hash Map for sorted data
BST keeps elements sorted and supports efficient range queries, while Hash Map does not maintain order.
Final Answer:
Binary Search Tree (BST) -> Option A
Quick Check:
Sorted data + range queries = BST [OK]
Hint: Sorted order needs BST, hash maps don't keep order [OK]
Common Mistakes:
Thinking hash maps keep data sorted
Confusing stack or queue with sorted structures
Assuming hash maps support range queries
2. Which of the following is the correct way to declare a Hash Map (Map) in JavaScript?
easy
A. const map = {};
B. const map = Map();
C. const map = new HashMap();
D. const map = new Map();
Solution
Step 1: Recall JavaScript Map syntax
JavaScript uses the Map object with the syntax new Map() to create a hash map.
Step 2: Check each option
const map = new Map(); uses new Map(), which is correct. const map = Map(); misses 'new'. const map = {}; creates a plain object, not a Map. const map = new HashMap(); uses a non-existent HashMap class.
Final Answer:
const map = new Map(); -> Option D
Quick Check:
Map creation syntax = new Map() [OK]
Hint: Use new Map() to create a hash map in JavaScript [OK]
Common Mistakes:
Omitting 'new' keyword
Using plain objects instead of Map
Assuming HashMap class exists in JS
3. Consider the following JavaScript code using a BST class with insert and inorderTraversal methods:
const bst = new BST();
bst.insert(10);
bst.insert(5);
bst.insert(15);
console.log(bst.inorderTraversal());
What will be the output?
medium
A. [10, 5, 15]
B. [5, 10, 15]
C. [15, 10, 5]
D. [5, 15, 10]
Solution
Step 1: Understand inorder traversal of BST
Inorder traversal visits nodes in ascending order: left subtree, root, right subtree.
Step 2: Trace insertion and traversal
Insert 10 as root, 5 to left, 15 to right. Inorder traversal visits 5, then 10, then 15.
Final Answer:
[5, 10, 15] -> Option B
Quick Check:
Inorder traversal = sorted nodes [OK]
Hint: Inorder traversal of BST returns sorted array [OK]
Common Mistakes:
Assuming insertion order equals traversal order
Confusing preorder or postorder with inorder
Expecting hash map order instead of BST order
4. The following code attempts to use a Hash Map to maintain sorted keys. What is the main issue?
JavaScript Map preserves insertion order, not sorted order of keys.
Step 2: Analyze the output of keys()
Keys will be in insertion order [3,1,2], not sorted [1,2,3].
Final Answer:
Keys are not guaranteed to be in sorted order -> Option A
Quick Check:
Map keys order = insertion order, not sorted [OK]
Hint: Map keys keep insertion order, not sorted order [OK]
Common Mistakes:
Thinking Map sorts keys automatically
Believing Map keys must be strings
Confusing Map with Object key behavior
5. You need to store a large dataset with frequent insertions and lookups by key, but also require fast retrieval of all keys in sorted order. Which approach is best?
hard
A. Use an Array and perform linear search
B. Use a Hash Map alone and sort keys on demand
C. Use a Balanced BST to store keys and values
D. Use a Queue to maintain insertion order
Solution
Step 1: Analyze requirements for insertions, lookups, and sorted retrieval
Frequent insertions and lookups need efficient operations; sorted retrieval requires order maintenance.
Step 2: Compare data structures for these needs
Hash Map offers fast lookups but no order; sorting keys on demand is costly. Balanced BST maintains sorted order and supports efficient insertions and lookups.
Final Answer:
Use a Balanced BST to store keys and values -> Option C
Quick Check:
Balanced BST = fast insert, lookup + sorted keys [OK]
Hint: Balanced BST combines fast lookup and sorted order [OK]
Common Mistakes:
Choosing hash map without sorting cost consideration
Using array causing slow lookups
Using queue which doesn't support sorted retrieval
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.