Hash Table Flashcards

1
Q

What is hash functions?

A

A hash function is a function where you put in a string and you get back a number.
“maps strings to numbers.”
requirements for a hash function:
• It needs to be consistent. For example, suppose you put in “apple” and get back “4”. Every time you put in “apple”, you should get “4” back. Without this, your hash table won’t work.
• It should map different words to different numbers. For example, a hash function is no good if it always returns “1” for any word you put in. In the best case, every different word should map to a different number.

it assigns a spot in the array alphabetically.

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

What is hash table?

A

It’s a Data structure, combined array and hash functions,
Hash table have keys and value,

  • hashes are good for:
    • Modeling relationships from one thing to another thing
    • Filtering out duplicates
    • Caching/memorizing data instead of making your server do work
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is collisions?

A

It’s when two values assign to the same key,

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

What the solution collision?

A

The simplest solution is this: if multiple keys map to the same slot, start a linked list at that slot.

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

What is the “constant time”?

A

It’s where algorithm take o(1), that’s mean that the time taken to perform will remain the same

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

What in the big O for hash table?

A

In average case => O(1)
In worst case => O(n)

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

How to avoid collision?

A

To avoid collisions, you need

• A low load factor
• A good hash function

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

What is “ load factor”?

A

It’s measures how many empty slots remain in your hash table.
Load factor = No. of items in the hash table / total No. of slots
Having a load factor greater than 1 means you have more items than slots in your array.

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

What is resizing?

A

It’s when you add more slots to your hash table
resize when your load factor is greater than 0.7

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

What is the good and bad hash function?

A

The good hash function distributes values in the array evenly.
The bad hash function groups values together and produces a lot of collisions.

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