Hash Maps Flashcards

1
Q

hash map

A

unordered associations between keys and values

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

hashing

A

process of mapping a key to its location

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Hash Table

A

a storage that holds the records (the key and any value associated with the key)
-usually implemented internally using an array. Each slot in the array holds a key-value pair or is empty (null).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Hash Function

A

maps keys to positions in the hash table

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

standard hash function

A
class HashMap {
    constructor(initialCapacity=8) {
        this.length = 0;
        this._hashTable = [];
        this._capacity = initialCapacity;
        this._deleted = 0;
    }
    static _hashString(string) {
        let hash = 5381;
        for (let i = 0; i < string.length; i++) {
            hash = (hash << 5) + hash + string.charCodeAt(i);
            hash = hash &amp; hash;
        }
        return hash >>> 0;
    }
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

collisions

A

2 unique keys hash to the same slot in the array

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

open addressing

A

when you have a collision, you hash the key to the empty slot nearest to where it should live

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

separate chaining

A

when you have a collision, we use the next pointers to put the keys in a linked list.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

set() method

A
set(key, value){
        const loadRatio = (this.length + this._deleted + 1) / this._capacity;
        if (loadRatio > HashMap.MAX_LOAD_RATIO) {
            this._resize(this._capacity * HashMap.SIZE_RATIO);
        }
        //Find the slot where this key should be in
        const index = this._findSlot(key);
        if(!this._hashTable[index]){
            this.length++;
        }
        this._hashTable[index] = {
            key,
            value,
            DELETED: false
        }; 
    }
    _findSlot(key) {
        const hash = HashMap._hashString(key);
        const start = hash % this._capacity;
    for (let i=start; i
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

resize()

A
_resize(size) {
        const oldSlots = this._hashTable;
        this._capacity = size;
        // Reset the length - it will get rebuilt as you add the items back
        this.length = 0;
        this._hashTable = [];
        for (const slot of oldSlots) {
            if (slot !== undefined) {
                this.set(slot.key, slot.value);
            }
        }
    }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly