Data Structures Flashcards

Arrays, Linked List, Hash Table, Stack, Queue, Heap, Binary Tree

1
Q

array

A

a collection of data elements, linear order is given by their physical placement in memory

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

array: index (get)

A

O(1)

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

array: insert/remove at the end

A

amortized O(1) (for dynamically resizing array)

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

array: insert/remove at any position

A

O(n)

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

array: insert/remove at the beginning

A

O(n)

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

linked list

A

a collection of data elements not stored in adjacent physical memory. a group of nodes representing a sequence. each node is composed of data element itself and a pointer to the next node in the sequence.

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

linked list: insert/remove at any position

A

O(1)

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

linked list: index (get)

A

O(n)

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

binary tree

A

a tree with each node having at most two children

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

use cases of binary tree

A
  • a way to access nodes based on value or label associated with each node. e.g. BST, binary heap (searching and sorting)
  • as a representation of data with a relevant bifurcating structure where the particular arrangement of nodes under and/or to the left or right of other nodes is part of the information (that is, changing it would change the meaning).
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

balanced tree

A

|height(left) - height(right)| <= 1 for any node

height = O(log n)

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

binary tree: insert a leaf

A

O(1)

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

binary tree: remove a zero/single child node

A

O(1)

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

traversal of binary trees

A

pre-order, in-order, post-order
breath-first order
depth-first order

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

implementation of binary trees

A

nodes and pointers

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

binary search tree

A

sorted binary tree, node.val is greater or equal than all the nodes in the left subtree, node.val is less or equal than all the nodes in the right subtree

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

binary search tree: height

A

balanced bst: O(log n); any bst: O(n)

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

binary search tree: search

A

average O(log n), worst case O(n)

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

binary search tree: insert

A

average O(log n), worst case O(n)

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

binary search tree: remove

A

average O(log n), worst case O(n)

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

self-balancing binary search tree

A

AVL and black-red tree

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

AVL

A

Re-balance the tree by performing appropriate rotations on the subtree

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

black-red tree

A

1) Every node has a color either red or black.
2) Root of tree is always black.
3) There are no two adjacent red nodes (A red node cannot have a red parent or red child).
4) Every path from a node (including root) to any of its descendant NULL node has the same number of black nodes.
In Red-Black tree, we use two tools to do balancing.
1) Recoloring
2) Rotation

24
Q

use case of binary search tree

A

lookup table, priority queue, dynamic sets

25
Q

hash table

A

map between key and value, a hash function hash(key) = index, pointing to a buckets/slots contains the corresponding value(s)

26
Q

collision of hash table

A

multiple key to the same index

27
Q

hash table: get

A

O(1)

28
Q

hash table: insert

A

amortized O(1)

29
Q

hash table: remove

A

amortized O(1)

30
Q

use case of hash table

A

associative arrays, database indexing, caches, and sets

31
Q

hash table: choice of hash function

A

uniform distribution

32
Q

hash table: collision resolution

A
  • separate chaining: a linked list in each bucket
  • open addressing: all entry records are stored in the bucket array itself. When a new entry has to be inserted, proceed in some probe sequence, until an unoccupied slot is found. search is the same procedure
33
Q

hash table: dynamic resizing

A

copy all the entries to the new array when the size get bigger than a threshold, incremental resizing

34
Q

queue

A

FIFO, addition of entities to the rear terminal position, known as enqueue, and removal of entities from the front terminal position

35
Q

implementations of queue

A
circular buffer (big array with head and tail floating around)
linked list with tail pointer
deque
36
Q

queue: insert and delete (enqueue and dequeue)

A

O(1)

37
Q

stack

A

last-in-first-out

38
Q

stack: push

A

from top, O(1)

39
Q

stack: pop

A

from top, O(1)

40
Q

stack: peek

A

O(1)

41
Q

implementations of stack

A

array

linked list

42
Q

use cases of stack

A

expression evaluation and syntax parsing
backtracking, depth-first-search
memory

43
Q

what is stack overflow?

A

Parameters and local variables are allocated on the stack (with reference types, the object lives on the heap and a variable in the stack references that object on the heap). The stack typically lives at the upper end of your address space and as it is used up it heads towards the bottom of the address space (i.e. towards zero).
Your process also has a heap, which lives at the bottom end of your process. As you allocate memory, this heap can grow towards the upper end of your address space. As you can see, there is a potential for the heap to “collide” with the stack.

44
Q

priority queue

A

Each element has a “priority” associated with it. In a priority queue, an element with high priority is served before an element with low priority. If two elements have the same priority, they are served according to their order in the queue.

45
Q

implementations of priority queue

A

heap: O(n) to build, O(log n) to insert and remove
self-balanced BST: O(n log n) to build initially, O(log n) to insert and remove
unordered array: O(n^2)
ordered array: O(n^2)

46
Q

equivalence between priority queue and sorting

A
The semantics of priority queues naturally suggest a sorting method: insert all the elements to be sorted into a priority queue, and sequentially remove them; they will come out in sorted order.
heap -------------------- heap sort
self-balanced bst --- tree sort
unordered array ---- selection sort
ordered array -------- insertion sort
47
Q

priority queue: build

A

heap implementation O(n), self-balance bst implementation O(n log n)

48
Q

priority queue: push

A

O(n log n)

49
Q

priority queue: pop

A

O(n log n)

50
Q

use cases of priority queue

A

bandwidth management
dijkstra’s algorithm
hoffman coding
best-first search

51
Q

heap

A

A specialized tree-based data structure which is essentially an almost complete tree that satisfies the heap property:
max-heap: each parent is larger or equal than its children
min-heap: each parent is smaller or equal than its children
Visualized as a tree, but is usually linear in storage (array, linked list).

52
Q

heap: build

A

O(n)

53
Q

heap: insert top

A

O(n log n)

54
Q

heap: remove top

A

O(n log n)

55
Q

heapify

A

create a heap from an unsorted list of elements

56
Q

heap sort

A

sort a unsorted list of elements in place using max heap

min heap can not do in place

57
Q

use cases of heap

A
priority queue
k-way merge
selection algorithm
graph algorithms (dijkstra)
order statistics (get median/k-largest)