Week 3 + 4 + 5 Flashcards

1
Q

Define stack.

A

Container with insertion and removal based on LIFO (Last-In-First-Out).

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

Define push, pop, top with stack.

A

push, inserts onto top of stack
pop, removes off of top of stack
top is last item pushed.

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

How is stack implemented using array?

A

Top of stack is set to the size of the array - 1. pop reduces top by 1 push adds element to top + 1 space and increments top. top = -1 for empty stack.

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

How is stack implemented using linked list?

A

Top of stack is set to lastNode added. pop sets top to lastNode->nextnode. push creates newNode with next lastNode and sets top to newNode. top points to null for empty stack.

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

Explain String Builder syntax. Why use string builder?

A

StringBuilder sb = new StringBuilder();
sb.append(“String”);

+ all other normal string functions.

String builder is mutable so a new String is not created after every modification unlike with normal Strings.

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

Define queue.

A

Container with insertion at back and removal from front. Based on FIFO (First-In-First-Out) data structure.

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

Define enqueue, dequeue and front for queue.

A

Enqueue inserts at rear of queue
Dequeue removes the element at the front of the queue.
Front returns the front of the queue.

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

How is queue implemented using array.

A

Front variable keeps track of front of queue, rear location is always empty. Enqueue grows the rear and dequeuing grows the front variable. Elements crawl to right in array as we modify the queue. We use circular arrays so when we reach the end of the array the elements circle back to the start.

Implementing Circular Array:
For enqueue: Insertion position set to (front + size) % CAPACITY
For dequeue: Front set to (front + 1) % CAPACITY.

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

How is queue implemented using linked list?

A

Doubly Linked List used. Enqueue, performs addLast, Dequeue performs removeFirst.

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

Define deque.

A

A queue where we can insert and delete from both the front and the end.

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

Define a tree.

A

A tree consists of nodes, where each node (except the first) has a parent and all nodes have zero or more children.

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

Define root.

A

First node which has no parent.

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

Define internal node.

A

A node with at least one child.

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

Define external node.

A

A node without children.

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

Define subtree.

A

Tree within a tree consisting of a node and its descendants.

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

Define depth of node.

A

Number of ancestors of p other than p itself.

17
Q

Define height of tree.

A

Maximum depth of any node. The height of its root node.

18
Q

What is the ordering in a tree?

A

Siblings are arranged left to right, from lowest to greatest.

19
Q

Define binary tree.

A

A tree consisting of a single node or a tree whose root has an ordered pair of children each of which is a binary tree.

20
Q

Define height of a node.

A

Number of edges on longest path from the node to a leaf which has a height of 0. Defined recursively. H (Node) = 1 + Height (child node). Where H (leaf) = 0

21
Q

Define proper or full binary tree.

A

A binary tree where each node has either zero or two children.

22
Q

T or F. In a binary node depth d has at most 2^d nodes.

23
Q

In a non-empty binary tree what is the minimum and maximum number of nodes n.

A

h + 1 <= n <= 2^(h + 1) - 1

24
Q

In a non-empty binary tree what is the minimum and maximum number of external nodes.

A

1 <= ne <= 2^h

25
Q

In a non-empty binary tree what is the minimum and maximum number of internal nodes.

A

h <= ni <= 2^h - 1

26
Q

In a non-empty binary tree what is the minimum and maximum height in terms of the nodes.

A

log(n + 1) - 1 <= h <= n - 1

27
Q

In a non-empty PROPER binary tree, what is the number of external nodes.

A

ne = ni + 1

28
Q

Explain inorder traversal.

A

Left subtree -> Node -> Right subtree

29
Q

Explain preorder traversal.

A

Node -> Left subtree -> Right subtree. Moves top to bottom.

30
Q

Explain postorder traversal.

A

Left subtree -> Right subtree -> Node. Moves bottom to top.

31
Q

Explain breadth-first traversal.

A

Pushes all nodes at a particular depth onto stack and visits them first before moving down to next level. Moves top to bottom.

32
Q

What is the Catalan number?

A

The number of full binary trees with n + 1 leaves.

Cn = 1 / n+1 (2n C n)

33
Q

What is the expected height for a random binary tree of size n = 30.

A

3.3 log (30) = 11

34
Q

Distinguish between iterable and iterator.

A

Iterable:
Represents a collection that can be iterated over using a for-each loop.
When implementing an Iterable, we need to override the iterator() method.
Doesn’t store the iteration state.
Removing elements during the iteration isn’t allowed.

Iterator:
Represents an interface that can be used to iterate over a collection.
When implementing an Iterator, we need to override the hasNest() and next() methods.
Stores the iteration state
Removing elements during the iteration is allowed.

35
Q

Distinguish between the lazy and snapshot implementation of iterator.

A

Snapshot:
Maintains its own private snapshot of element sequence
Constructed when iterator object is created
Unaffected by subsequent changes to original

Lazy:
Does not make an upfront copy.
Piecewise traversal of original.
Affected by subsequent changes to original.