Lecture
Coding Quiz
In this coding quiz, you will implement a custom hash table and use it to perform specific tasks within a program.
The hash table should store key-value pairs and provide functionality to retrieve values for given keys.
You will use the hash table to build a program that counts the occurrences of each character in a given string.
Hash Table Code Template
class HashTable: def __init__(self): self.size = 256 self.table = [[] for _ in range(self.size)] def put(self, key, value): hash_key = hash(key) % self.size for item in self.table[hash_key]: if item[0] == key: item[1] = value return self.table[hash_key].append([key, value]) def get(self, key): hash_key = hash(key) % self.size for item in self.table[hash_key]: if item[0] == key: return item[1] return None def count_characters(self, string): # Write your code here return # Write your code here def solution(s): hash_table = HashTable() return hash_table.count_characters(s)
Constraints
- The size of the hash table is fixed and set to 256.
Example Input/Output
-
Input:
"hello" -
Output:
{'h': 1, 'e': 1, 'l': 2, 'o': 1}
Lessons in this chapter · Introduction to Data Structures / Algorithms - Time Complexity, Space Complexity, Arrays, Stacks, Queues, Linked Lists, Hash Tables
- 1. The Core of Programming - Data Structures and Algorithms
- 2. What is Algorithm Complexity?
- 3. Time and Space Complexity of Algorithms
- 4. Storing Data Sequentially with Array
- 5. Coding Quiz - Implementing an Array
- 6. Stack: Data Entered Last Comes Out First
- 7. Coding Quiz - Implementing a Stack
- 8. Queue: First In, First Out Data Structure
- 9. Coding Quiz - Implementing a Queue
- 10. Creating a Linked List Structure Using Nodes
- 11. Coding Quiz - Implementing a Linked List
- 12. Storing Key-Value Pairs with Hash Table
- 13. Coding Quiz - Implementing a Hash Table
Lecture
AI Tutor
Design
Upload
Notes
Favorites
Help