I was asked to implement a hash map in this phone interview (screen-share), so time was limited and this is what I came up with. I'd appreciate if someone can take a minute to critique/review it and help me improve.
Here is an online version of the code.
#include <list>
#include <iostream>
using namespace std;
const int SIZE = 10;
class Node{
public:
Node(){}
Node(int k, int v):key(k), value(v){}
int key, value;
};
class HashMap {
private:
list<Node*> data[SIZE];
public:
~HashMap();
Node* get(int key);
void put(int key, int value);
int hashFn(int val){ return val % 13; }
};
Node* HashMap::get(int key){
int hashValue = hashFn(key);
int bucket = hashValue % SIZE;
list<Node*>::iterator it = data[bucket].begin();
while(it != data[bucket].end()){
Node ** d = &(*it);
if((*d)->key == key){
return *d;
}
it++;
}
return NULL;
}
void HashMap::put(int key, int value){
int hashValue = hashFn(key);
int bucket = hashValue % SIZE;
Node* node = this->get(key);
if(node == NULL){
data[bucket].push_front(new Node(key, value));
}
else{
node->value = value;
}
}
HashMap::~HashMap(){
for(int i = 0; i < SIZE; ++i){
list<Node*>& val = data[i];
for(list<Node*>::iterator it = val.begin(); it != val.end(); it++){
Node* n = *it;
delete n;
}
}
}
int main(){
HashMap map;
cout << "Finding 5: " << map.get(5) << endl; // -1
map.put(5, 10);
map.put(5, 11);
cout << "Finding 5: " << map.get(5)->value; // 11
return 1;
}