Data Structures and Algorithms Flashcards

1
Q

What are 2 different approaches for deciding when to do work?

A

Just-In-Time: Waits for a request before doing any work.
Ahead-of-Time: Does all possible work beforehand so it can quickly access results once requested.

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

Name 3 examples when logs come up in algorithms?

A

1) Binary Search = O(logn)
2) Sorting = O(nlogn)
3) Height of Tree = O (logn+1)

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

What is the equation for the height of a binary tree?

A

h= log(n+1)

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

What is the equation for the number of nodes on the last line of a binary tree?

A

n= 2^h -1

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

What is a recursively-defined data structure and give some examples?

A

A data structure that can be defined using itself.

  • The linked list can be defined as a data structure consisting of an object referencing a list (or null).
  • Trees like HTML elements tree or an object made of a company’s departments (and subdepartments) are also naturally recursive: they branch and every branch can have other branches.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Describe some benefits and drawbacks of using a linked list vs an array.

A

The “delete element” and “insert element” operations for arrays are expensive. For instance, arr.unshift(obj) operation has to renumber all elements to make room for a new obj, and if the array is big, it takes time. Same with arr.shift(). Push/pop only works at the end.
Linked list have very fast insertion/deletion. Unlike arrays, there’s no mass-renumbering, we can easily rearrange elements.

The main drawback of linked lists is that we can’t easily access an element by its number. In an array that’s easy: arr[n] is a direct reference. But in the list we need to start from the first item and go next N times to get the Nth element.

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

What is a linked list?

A

The linked list element is recursively defined as an object with:

  • value.
  • next property referencing the next linked list element or null if that’s the end.
Example:
let list = {
  value: 1,
  next: {
    value: 2,
    next: {
      value: 3,
      next: {
        value: 4,
        next: null
      }
    }
  }
};
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

When should you use Array.from over Array.map when iterating over array?

A

Array.from iterates over each element from 0 to the length of the array, while map only iterates over properties that are actually on the array. If you need to iterate over empty/undefined values in the array, use Array.from

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

What are the Strengths and Weaknesses of Linked Lists. Ideal times to use them?

A

Strengths:
-Fast operations on the ends
-Flexible size

Weaknesses:
-Costly lookups compared to arrays. You have to take O(i) to get to the ith item
-Can take longer to iterate through than arrays because caching systems make reading from sequential addresses in memory faster (though they are both O(n))

Uses: Stacks and queues only need fast operations on the ends, so linked lists are ideal

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

What is the worst Big O for Linked Lists?
(space, prepend, append, lookup, insert, delete)

A

space: O(n)
prepend: O(1)
append: O(1)
lookup: O(n)
insert: O(n)
delete: O(n)

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

What are the benefits and drawbacks of queue structures? Ideal times to use them?

A

Strengths: Fast operations! (all O(1))

Uses:
-Breadth-first search: keeps track of the nodes to visit next
-Printers: uses queues to manage jobs
-Web servers: uses queues to manage requests
-Processes: wait in the CPU scheduler’s queue for their turn

Easy to implement with Linked lists!

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

What values should you construct a queue class with?

A

First and Last; Optional: length
Often times assign first and last to a node for a linked list.
Can also assign first and last to two arrays (2 stacks to make queue problem)

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

What is Breadth-First Search? What are the advantages and disadvantages?

A

A method of exploring a tree or graph. You first visit all of the immediate children, hen move on to all those node’s children, etc until we reach the end.

Advantage:
-BFS will find the shortest path

Disadvantage:
-BFS generally requires more memory than DFS

BFS uses a queue to keep track of the nodes to visit next

Uses:
-Shortest Path
-Most related items, i.e. closest connections or friends
-Working with information on upper level

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

What is Depth-First Search? What are the advantages and disadvantages?

A

A method of exploring a tree or graph. You go as deep as possible down one path before backing up and trying a different one.

Advantages:
-Generally less memory than DFS
-Can be easily implemented with recursion

Disadvantages:
-DFS doesn’t necessarily find shortest path

DFS uses a stack

Uses:
-Figure out if a path exists
-Comparing leaf nodes
-Working with information on the lower level of tree

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