CS50 Data Structures Flashcards

1
Q

Arrays (insertion, deletion, lookup, sort, size)

A

Insertion is bad - costs of shifting to fit an element in the middle
deletion is bad - lost of shifting after removing an element
lookup is great - random access, constant time
relatively easy to sort
relatively small size-wise
stuck with a fixed size, no flexibility

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

Linked Lists (insertion, deletion, lookup, sort, size)

A

Insertion is easy - just tack onto the front
deletion is easy - once you find the element
lookup is bad - have to rely on linear search
relatively difficult to sort - unless you’re willing to compromise on super-fast insertion and instead sort as you construct
relatively small size-wise (not as small as arrays)

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

What is depth first? What is the difference between depth first preorder, inorder, and post order?

A

Search all the nodes down one side of the tree before going to the other side

preorder
-root, left, right

inorder
-left,root,right

postorder
-left,right,root

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

What is breath first search?

A

visit every node at the current level

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

How do you reverse a linked list

A
  • set a newHead and a newTail
  • the newTail is instantiated and will always be the original head
  • the newHead is instantiated as the next of head
  • the newHead will be the next of the newTail at the end of each loop iteration
  • loop through the nodes
  • set a temp variable to the .next of the newHead
  • point newHead to the head
  • point newTail (the original head) to temp
  • set head to newHead
  • set newHead to temp
  • return head
var reverseList = function(head) {
    if(!head || !head.next) return head
    let newTail = head
    let newHead = head.next
    while (newHead !== null){
      let temp = newHead.next
      newHead.next = head
      newTail.next = temp
      head = newHead
      newHead = temp
    }

return head
};

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

What is an AVL tree?

A

AVL tree is a self-balancing Binary Search Tree (BST) where the difference between heights of left and right subtrees cannot be more than one for all nodes.

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

What type of ordered does a stack have?

A

last-in first-out LIFO

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

What type of ordering does a queue have?

A

first-in first-out FIFO

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

What is the difference between a binary search tree and a binary tree?

A

A binary search tree imposes the condition that, for each node, its left descendents are less than or
equal to the current node, which is less than the right descendents.

A binary tree does not follow this order of placing nodes. larger numbers and smaller number appear both on the left and right

The definition of a binary search tree can vary slightly with respect to equality. Under some definitions,
the tree cannot have duplicate values. In others, the duplicate values will be on the right
or can be on either side. All are valid definitions, but you should clarify this with your interviewer.

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

What is a complete binary tree?

A

A complete binary tree is a binary tree in which every level of the tree is fully filled except for perhaps the last level. To the extent that the last level is filled, it is filled left to right.

All the nodes of the tree must be filled. However the most right node of the right branch can not be filled and it is still considered complete.

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

What is a full binary tree?

A

A full binary tree is a binary tree in which every node has either zero or two children. That is, no nodes have
only one child.

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

What is a perfect binary tree?

A

A perfect binary tree is one that is both full and complete. All leaf nodes will be at the same level, and this level has the maximum number of nodes.

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

What is are Tries (Prefix Trees)?

A

A trie is a variant of an n-ary tree in which characters are stored at each node. Each path down the tree may
represent a word.

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