Data Structures & Algorithms Flashcards

1
Q

Swift collection protocols

A

“Tier 1, Sequence: A sequence type provides sequential access to its elements. This axiom comes with a caveat: Using the sequential access may destructively consume the elements.

Tier 2, Collection: A collection type is a sequence type that provides additional guarantees. A collection type is finite and allows for repeated nondestructive sequential access.

Tier 3, BidirectionalColllection: A collection type can be a bidirectional collection type if it, as the name suggests, can allow for bidirectional travel up and down the sequence. This isn’t possible for the linked list, since you can only go from the head to the tail, but not the other way around.

Tier 4, RandomAccessCollection: A bidirectional collection type can be a random access collection type if it can guarantee that accessing an element at a particular index will take just as long as access an element at any other index. This is not possible for the linked list, since accessing a node near the front of the list is substantially quicker than one that is further down the list.

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

Performance analysis of linked list

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

Explain linked list

A

It has head

It has tail

Unidirectional

Methods:

push: Adds a value at the front of the list.
append: Adds a value at the end of the list.

insert(after:): Adds a value after a particular node of the list.

After adopting Sequence and Collection gets:

push

append

insert(after:)

pop

removeLast

remove(after:)

Has copy-on-write value semantic (COW) that use isKnownUniquelyReferenced

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

Linked-list queue strengths and weaknesses and Complexity table

A

Despite O(1) performance, it suffers from high overhead. Each element has to have extra storage for the forward and back reference. Moreover, every time you create a new element, it requires a relatively expensive dynamic allocation. By contrast, QueueArray does bulk allocation, which is faster.

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

In-order traversal

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

Pre-order traversal

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

Explain Binary Search Tree

A

two rules on the binary tree:

The value of a left child must be less than the value of its parent.

Consequently, the value of a right child must be greater than or equal to the value of its parent.

Lookup, insertion and removal is O(log n) if tree is balanced

The binary search tree is a powerful data structure for holding sorted data.

Elements of the binary search tree must be comparable. This can be achieved using a generic constraint or by supplying closures to compare with.

The time complexity for insert, remove and contains methods in a BST is O(log n).

The performance will degrade to O(n) as the tree becomes unbalanced. This is undesirable, so you’ll learn about a self-balancing binary search tree called the AVL tree

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

Graph analysis matrix vs list

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

Types of time complexity

A

Constant time, O(1)
logarithmic time, O(log n)
linear time, O(n)
quasi-linear time O(n log n)
quadratic time O(n2)

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

If you try to add new elements to an array that is already at maximum capacity

A

Array must restructure itself to make more room for more elements. This is done by copying all the current elements of the array in a new and bigger container in memory. However, this comes at a cost; Each element of the array has to be visited and copied.

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

Explain queues

A

“Array-based
Linked list
Ring buffer
Stack based”
Deque

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

Common operations for queue:

A

enqueue: Insert an element at the back of the queue. Returns true if the operation was successful.
dequeue: Remove the element at the front of the queue and return it.

isEmpty: Check if the queue is empty.

peek: Return the element at the front of the queue without removing it.

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

Array-based queue strengths and weaknesses and Complexity table

A

Can be inefficient for very large queues

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

Ring buffer implementation

A

if read & write is at the same index -> it means that queue if full or empty

fixed size

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

Double-stack implementation

A

Dynamic
dequeue is amortised O(1)
Array elementts are next to each other in memory blocks -> loaded in a cache.
Array require O(n) for copy, but it’s very fast O(n)

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

Compared to other data structures, leveraging two stacks improves

A

the dequeue(_:) time complexity to amortized O(1) operation.

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

double-stack implementation beats out Linked-list in terms

A

of spacial locality.

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

Space complexity: Array vs linked list queues

A

Elements in an array are laid out in contiguous memory blocks, whereas elements in a linked list are more scattered with potential for cache misses.

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

Post-order traversal

A
20
Q

Explain Adjacency matrix graph

A

uses a square matrix to represent a graph

good for dense graphs, when your graph has lots of edges

21
Q

Objects of graph

A

Vertex
Edge

22
Q

Time complexity

A

“is a measure on the time required to run an algorithm as the input size increases.”

23
Q

Space complexity

A

is a measure of the resources required for the algorithm to run.

24
Q

Big O notation is used to represent

A

the general form of time and space complexity.

25
Q

Time and space complexity are

A

high-level measures of scalability; they do not measure the actual speed of the algorithm itself.

26
Q

For small data sets, time complexity is usually

A

irrelevant. A quasilinear algorithm can be slower than a linear algorithm.

27
Q

Types of Data Structures & Algorithms complexities

A

Time complexity
Space complexity

28
Q

How much memory an array takes?

A

Underneath the hood, Swift arrays are allocated with a predetermined amount of space for its elements.

29
Q

Array vs Dictionary operations and time cost

A
30
Q

If you find yourself needing to use insert(at:) frequently with indices near the beginning of the array

A

you may want to consider a different data structure such as the linked list.”

31
Q

Copy-on-write behavior

A
32
Q

head first complexity insertion for array and list

A

Linked lists have a O(1) time complexity for head first insertions. Arrays have O(n) time complexity for head-first insertions

33
Q

runner’s technique

A

One is running x2 and when he reaches the end of the list, the slower runner will be in the middle

Finding the middle of list

34
Q

What to think about when you create new algorithm?

A

Space complexity -> if you need allocate more objc
Time complexity -> how many objc you need iterate

35
Q

What ordering queues use?

A

FIFO first in first out

36
Q

How blocks of memory placed in linked list vs stack/array

A
37
Q

Enqueue vs Dequeue

A

Enqueue inserts an element to the back of the queue.
Dequeue removes the element at the front of the queue.

38
Q

Ring-buffer-queue based implementation is good

A

for queues with a fixed size.

39
Q

Tree vs linked-list

A

linked-list nodes may only link to one successor node,

a tree node can link to many child nodes.

40
Q

Type of traversals

A

depth-first and level-order

41
Q

Explain Binary Tree

A

binary trees that impose restrictions on the insertion/deletion behaviors

42
Q

AVL Trees explain

A

It looks like Search Treen, but it balanced itself

Type of balance:
Perfect Balance
Good Balance
Unbalanced

A self-balancing tree avoids performance degradation by performing a balancing procedure whenever you add or remove elements in the tree.

43
Q

how to measuring balance in AVL tree?

A

Find height of node and see if left&right child are differ

44
Q

How AVL balanced itself?

A

Balance is achieved by four types of tree rotations on node insertion and removal.

45
Q

How AVL preserve balance?

A

AVL trees preserve balance by readjusting parts of the tree when the tree is no longer balanced.

46
Q

Explain Adjacency list graph

A

stores a list of outgoing edges for every vertex

good for sparse graphs, when your graph has the least amount of edges

47
Q
A