hash table Flashcards

1
Q

describe operation of adding key-val pair

A

hash(key) to get hash index
insert & rec key into hash table at hash index

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

hash collision def

A

when diff keys have same hash index

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

good hash function (hint: data, key, hash values)

A
  • must use all input data
  • uniformly distributes hash vals across entire set of possible hash values
  • small difference in data causes a large difference in hash values
    OR
    generates very different hash values for similar (but non-identical) keys
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

conflict resolution (open addressing)

A
  • rehashing function will be called continuously until diff/new location found
  • key-value pair will be stored at diff/new location
  • lookups for keys should check subsequent locations if first key does not match
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

conflict resolution (closed addressing)

A
  • linked list can be instantiated at each array position
  • key-value pairs are stored in linked list at each hash position
  • lookup for keys done by walking through the linked list, checking for matching key
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

application: why hash table can’t be a stack

A
  • hash table does not remember order of insertion
  • each item is hashed to a location that is unique to the index & not in running order
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

advantage of hash table

A

time complexity O(1), constant time access as input data grows

(assuming hash collisions x occur v often, mention it esp if prev part of qn talked abt it)

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

if hash table size n, exp how >n entries wld be stored in hash table

A
  • the current hash table is unable to do so. :. a new hash table w larger capacity has to be created
  • current entries have to be extracted, rehashed w the new size & inserted into the new hash table
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

hash table vs bst

A

(context: 2023 nyjc prelim 3cii, prev parts mentioned unbalanced tree + hash collision :. need mention in final eval)

  • for a large variety of keys, the % of hash collisions wld incr → collision resolution cld incr t complexity of inserting frm O(1) to O(n)
  • while for BST, t complexity is O(logn) but can worsen till O(n) if tree is maximally unbalanced whr it can undergo rebalancing. :. given t complexity comparison, BST more suitable
How well did you know this?
1
Not at all
2
3
4
5
Perfectly