Implement Hash Map Using Primitive Types
A small coding test that I encountered today.
Question
Using only primitive types, implement a fixed-size hash map that associates string keys with arbitrary data object references (you don't need to copy the object). Your data structure should be optimized for algorithmic runtime and memory usage. You should not import any external libraries, and may not use primitive hash map or dictionary types in languages like Python or Ruby.
The solution should be delivered in one class (or your language's equivalent) that provides the following functions:
- constructor(size): return an instance of the class with pre-allocated space for the given number of objects.
- boolean set(key, value): stores the given key/value pair in the hash map. Returns a boolean value indicating success / failure of the operation.
- get(key): return the value associated with the given key, or null if no value is set.
- delete(key): delete the value associated with the given key, returning the value on success or null if the key has no value.
- float load(): return a float value representing the load factor (`(items in hash map)/(size of hash map)`) of the data structure. Since the size of the dat structure is fixed, this should never be greater than 1.
If your language provides a built-in hashing function for strings (ex. `hashCode` in Java or `__hash__` in Python) you are welcome to use that. If not, you are welcome to do something naive, or use something you find online with proper attribution.
Solution
Key to construct a hash map is to construct several key-value entries.
1 package datastructures.map; 2 3 public class MyEntry<K, V> { 4 private final K key; 5 private V value; 6 7 public MyEntry(K key, V value) { 8 this.key = key; 9 this.value = value; 10 } 11 12 public K getKey() { 13 return key; 14 } 15 16 public V getValue() { 17 return value; 18 } 19 20 public void setValue(V value) { 21 this.value = value; 22 } 23 }
1 package datastructures.map; 2 3 // import datastructures.map.MyEntry; 4 5 public class MyMap<K, V> { 6 private int fullSize; 7 private int size; 8 private int DEFAULT_CAPACITY = 16; 9 10 private MyEntry<K, V>[] map = new MyEntry[DEFAULT_CAPACITY]; 11 12 public MyMap(int size) { 13 this.size = 0; 14 this.map = new MyEntry[size]; 15 this.fullSize = size; 16 } 17 18 public V get(K key) { 19 for (int i = 0; i < size; i++) { 20 if (map[i].getKey().equals(key)) { 21 return map[i].getValue(); 22 } 23 } 24 return null; 25 } 26 27 public boolean set(K key, V value) { 28 boolean insert = true; 29 for (int i = 0; i < size; i++) { 30 if (map[i].getKey().equals(key)) { 31 map[i].setValue(value); 32 insert = false; 33 } 34 } 35 if (insert) { 36 if (size >= fullSize) { 37 return false; 38 } 39 else { 40 map[size] = new MyEntry(key, value); 41 size++; 42 } 43 } 44 return true; 45 } 46 47 public V delete(K key) { 48 for (int i = 0; i < size; i++) { 49 if (map[i].getKey().equals(key)) { 50 V value = map[i].getValue(); 51 map[i] = null; 52 condenseArray(i); 53 return value; 54 } 55 } 56 return null; 57 } 58 59 public float load() { 60 return (float)size / (float)fullSize; 61 } 62 63 private void condenseArray(int start) { 64 size--; 65 for (int i = start; i < size; i++) { 66 map[i] = map[i + 1]; 67 } 68 } 69 70 }
1 package datastructures.map; 2 3 public class Test { 4 public static void main(String[] args) { 5 int size = 8; 6 MyMap<Character, Integer> test = new MyMap<Character, Integer>(size); 7 test.set('a', 1); 8 test.set('b', 2); 9 test.set('c', 3); 10 test.set('d', 4); 11 System.out.println(test.get('a')); 12 System.out.println(test.set('a', 2)); 13 System.out.println(test.get('a')); 14 System.out.println(test.load()); 15 test.delete('b'); 16 System.out.println(test.load()); 17 } 18 }
More basic knowledge about Java
Package and Import
Classes under same package need not import each other.
Here, we set three classes in package datastructures.map, and my current path is /home/Desktop
Then, I need to make a directory of datastructures/map under Desktop and put three source codes in that directory.
Meanwhile, I need to compile under Desktop directory.
To compile the Java programs with package statements you have to do use -d option as shown below.
javac -d Destination_folder file_name.java
There can be only one package statement in each source file, and it applies to all types in the file.
Two major results occur when a class is placed in a package:
1. The name of the package becomes a part of the name of the class, as we just discussed in the previous section.
2. The name of the package must match the directory structure where the corresponding bytecode resides.
Constructor
Constructor for object should not include inner types.
For example
public MyMap<K, V>() {} is wrong!
public MyMap() {} is right!
Public Class
(referrence: Tutorialspoint)
Four access level for classes:
1. Visible to the package. the default. No modifiers are needed.
2. Visible to the class only (private).
3. Visible to the world (public).
4. Visible to the package and all subclasses (protected).
Access Control and Inheritance:
1. Methods declared public in a superclass also must be public in all subclasses.
2. Methods declared protected in a superclass must either be protected or public in subclasses; they cannot be private.
3. Methods declared without access control (no modifier was used) can be declared more private in subclasses.
4. Methods declared private are not inherited at all, so there is no rule for them.