Skip to main content
Practice

Coding Quiz

In this coding quiz, you will implement a custom hash table and use it to perform specific tasks in a program.

The hash table you need to implement should store key-value pairs and provide functionality to retrieve values for given keys.

In this problem, you will use the hash table to create a program that calculates the number of 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}

Want to learn more?

Join CodeFriends Plus membership or enroll in a course to start your journey.