Data Structures Flashcards

1
Q

Hash Table

A

A hash table is a data structure that maps keys to values for highly efficient lookup.

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

Linked List

A

A data structure that represents a sequence of nodes

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

Singly Linked List

A

Each node points to the next node in the linked list

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

Doubly Linked List

A

Gives each node pointers to both the next node and the previous node

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

Runner Technique

A

The “runner” (or second pointer) technique is used in many linked list problems. The runner technique means that you iterate through the linked list with two pointers simultaneously, with one ahead of the other.

The “fast” node might be ahead by a fixed amount, or it might be hopping multiple nodes for each one node that the “slow” node iterates through.

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

Stack

A

Stack uses LIFO (last-in first out) ordering; most recent item added to the stack is the first item to be removed.

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

Stack Operations

A

pop(): remove the top item from the stack
push(item): add an item to the top of the stack
peek(): return the top of the stack
isEmpty(): return true if and only if the stack is empty

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

Stack offer constant-time access to the ith item?

A

False. Unlike an array.

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

Stack Constant time for add / remove

A

True. Constant time for push and remove. Does not require shifting elements around (unlike an array)

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

Queue

A

A queue implements FIFO (first-in first-out) ordering; items are removed from the data structure in the same order that they are added.

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

Queue Operations

A

add(item): add an item to the end of the queue
remove(): remove the first item in the queue
peek(): return the top of the queue
isEmpty(): return true if and only if the queue is empty

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

Trees

A

A tree is a data structure composed of nodes:

  • Each tree has a root node
  • Root node has zero or more child nodes
  • each child node has zero or more child nodes, and so on
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

The tree cannot contain cycles. The nodes may or may not be in a particular order, they could have any data type as values, and they may not have links back to their parent nodes

A

True

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

Binary Trees

A

a binary tree is a tree in which the node has up to two children. Not all trees are binary trees.

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

Node “leaf”

A

A node is called a “leaf” node if it has no children

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

Binary Search Tree

A

A binary search tree is a binary tree in which every node fits a specific ordering property: all left descendents <= n < all right descendents. This must be true for each node n.

17
Q

Balanced vs Unbalanced

A

With a balanced tree, access^1 is O(log n).
With an unbalanced tree, access^1 is O(n) (worst case).

an unbalanced tree built from sorted data is effectively the same as a linked list

18
Q

Two common types of balanced trees:

A

red-black trees, and AVL trees

19
Q

Complete Binary Trees

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 least level is filled, it is filled left to right.

20
Q

Full Binary Trees

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.

21
Q

Perfect Binary Trees

A

A perfect binary tree is one where all interior nodes have two children and all leaf nodes are at the same level.

22
Q

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.

23
Q
  • nodes in Tries (Prefix Trees)
A

sometimes called null nodes; often used to indicate complete words.

EG: the fact there is a * node under MANY indicates that MANY is a complete word. The existence of the MA path indicates there are words that start with MA.

24
Q

Graph

A

A graph is simply a collection of nodes with edges between (some of) them.

  • Graphs can be either directed or undirected.
25
Q

Trees are actually a type of graph, but not all graphs are trees

A

True. A tree is a connected graph without cycles

26
Q

Directed edges vs undirected edges (graphs)

A

Directed edges are like a one-way street, undirected edges are like a two-way street

27
Q

Adjacency List

A

Most common way to represent a graph; every vertex (or node) stores a list of adjacent vertices. In an undirected graph, an edge like (a, b) would be stored twice: once in a’s adjacent vertices and once in b’s adjacent vertices

28
Q

Adjacency Matrices

A

An adjacency matrix is an N x N boolean matrix (where N is the number of nodes), where a true value at matrix[i][j] indicates an edge from node i to node j.

29
Q

Two commons way to search a graph

A

Depth-first search, breadth-first search

30
Q

Depth-First Search (DFS)

A

start at the root (or another arbitrarily selected node) and explore each branch completely before moving on to the next branch.

That is, we go deep first (hence the name depth-first search) before we go wide.

31
Q

Breadth-first search (BFS)

A

start at the root (or another arbitrarily selected node) and explore each neighbor before going on to any of their children.

That is, we go wide (hence breadth-first search) before we go deep.

32
Q

What scenario to use DFS vs BFS?

A

DFS preferred if we want to visit every node in the graph; DFS a bit simpler overall.

BFS if we want to find the shortest path (or just any path) between two nodes; BFS is generally better.

33
Q

Bidirectional Search

A

used to find the shortest path between a source and a destination node. Operates by essentially running two simultaneous breadth-first searches, one from each node. When their searches collide, we have found a path (formed by merging the two paths).

Note: if the graph is directed, it searches forward from s and backwards from t (ie: running the opposite way on the edges)