Data Structures Flashcards

1
Q

What is a dictionary?

A

An abstract data type made up of keys and values in pairs

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

What are the 4 main operations of a dictionary?

A

CRUD: Create, Read, Update, Delete
1. Add a key-value pair
2. Delete a key-value pair
3. Update the value of a pair
4. Find the value of an associated key

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

Explain why an array is a random-access data structure

A

Random-access means that you can access an arbitrary element as fast as any other element regardless of where it is in the array.

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

How is the data of an array stored in RAM?

A

It’s stored in a contiguous block of memory where every element of the array has a fixed size known up front

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

Given the offset of the array, element size and element index, how do you calculate the address of the element?

A

element_address = offset + (element_size * element_index)

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

Explain how Python implements pointers to store values in arrays

A

Python uses pointers (integer numbers) in place of actual values in the array which reference the location of where the data is stored. That way, while the pointers are stored continguously for easy access, the actual values (with varying sizes) can be stored anywhere.

Pic

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

What does a hash function do?

A

Turns any data into a fixed-size sequence of bytes, called a hash value or hash code

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

T/F: a hash function with always return the same hash value for the same input, even if you exit and reopen Python

A

False, hash values are only immutable within the same Python session

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

What is a hash collision, and why is it unavoidable?

A

It’s when a hash function will yield the same hash value for two different values. It’s unavoidable because the goal of a hash function is to project a potentially infinite number of values onto a finite space (e.g. the pigeonhole principle: Given m items and n containers, if m > n, then there’s at least one container with more than one item.)

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

T/F: Good hash functions will distribute values evenly across available space

A

True

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

T/F: Hash functions distribute values pseudo-randomly

A

True

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

T/F: Hash functions are slow

A

False. They are lightning fast, hence why dictionaries are equally fast

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

T/F: You can reverse-engineer a hash function’s value back to the original value

A

False, it’s virtually impossible

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

T/F: Hash values from the same hash function can be different length numbers

A

False, they will always have a fixed-size

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

What makes a hash table’s key-value pairs unordered?

A

The hash function, because by nature it will try to evenly distribute the key-value pair across the allocated memory space

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

Briefly explain how to implement the 4 main operations of a hash table, given n fixed number of memory locations

A
  1. Set up a simple method of assigning an index value from the key’s hash value, i.e. idx = hash(key) % n
  2. Create: Use the index function on the key, then assign the key-value pair to the list of memory locations
  3. Read: Use the index function on the key, return the value from the key-value pair
  4. Update: Use the index function on the key, update the value from the key-value pair
  5. Delete: Use the index function on the key, remove the key-value pair from the list
17
Q

Why do hash tables have an O(1) T.C for lookup, insert, update, and delete functions?

A

Because in order to find/update/add/delete any key-value pair from a hash table, you only need to calculate the index location from the key, e.g. idx = hash(key) % memory_size as a simple example.

18
Q

Explain how linear probing is used to solve hash collisions

A
  1. Calculate the index of the key of a key-value pair
19
Q

T/F: For hash tables, you calculate the hash of the value in a key-value pair

A

False, you have to use the keys of the key-value pairs because they are guaranteed to be unique