Data Structures Flashcards

1
Q

Hash Table - what is it?

A

use hash code to insert a key and value by mapping the hash code to an index in an array or linked list

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

Retrieve from hash table

A

compute hash code from key, then index from hash code. Then search thru linked list for value with that key

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

Hash table run time

A

if collisions are high - O(N) N = number of keys.
generally, O(1)

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

Reverse Linked List - recursive

A

class Solution:
def reverseList(self, head):
if (not head) or (not head.next):
return head

    p = self.reverseList(head.next)
    head.next.next = head
    head.next = None
    return p
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Reverse Linked List - iterative

A

class Solution:
def reverseList(self, head):
prev = None
curr = head
while curr:
next_temp = curr.next
curr.next = prev
prev = curr
curr = next_temp

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

Pre-order Traversal - definition

A

Visit root first, then left, then right.

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

In-order Traversal - definition

A

Visit left first, then root, then right. SORTED ORDER

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

Post-order Traversal - definition

A

Left first, then right, then root. Deleting nodes in a tree will be in post-order - delete left child and right child before deleting node itself.

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

Level-order Traversal (BFS) - definition

A

traverse level by level. Start with root, then traverse neighbors, then the neighbor’s neighbors. Use queue

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

BFS runtime

A

O(vertices + edges)

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

DFS runtime

A

O(vertices + edges) use stack

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

Find depth of binary search tree

A

def depth(tree):
if not tree:
return 0
left_depth = depth(tree[‘left’])
right_depth = depth(tree[‘right’])

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