Grokking Algorithms Flashcards

1
Q

what is an algorithm?

A

it’s a set of instructions that accomplish a specific task,

any piece of code is an algorithm.

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

what do we use for searching?

A

binary search

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

what does a binary search need?

A

it needs a sorted list as it’s input

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

what is the big o of linear time (exhaustive search)

A

O(n)

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

what is the big O of binary search?

A

O(Log(n))

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

what does big O notation do?

A

it gives an indication of how fast an algorithm is running

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

why do we calculate big O notation?

A

because algorithms grow at different rates

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

what does big O notation calculate?

A

number of operations

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

what scenario does big O notations give?

A

worst-case scenario

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

what is big O for reading an array vs linked list?

A

array : O(1)

list : O(n)

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

what is big O for insertion an array vs linked list?

A

array : O(n)

list : O(1)

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

what is big O for deletion an array vs linked list?

A

array : O(n)

list : O(1)

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

what are the types of accessing elements in memory?

A

1 - random access

2 - sequential access

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

what does random access mean and who do we use it with?

A

we use it with an array and it means i can go to an exact index in memory

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

what does sequential access mean and who do we use it with?

A

we use it with linked lists and arrays, and it means that we have to go through the list reach time to reach a specific element or index

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

what is the big O notation for selection sort?

A

O(n^2)

17
Q

what is the difference in performance for loops and recursion?

A

loops : performance for program

recursion : performance for programmer

18
Q

what are the two parts for every recursive function?

A

1 - base case

2 - recursive case

19
Q

what are the two operations of a stack?

A

push and pop

20
Q

what is divide and conquer?

A

they are recursive algorithms

21
Q

what is the worst case and average case for quicksort?

A

worst : O(n^2)

average : n log n

22
Q

which is faster quick sort or merge sort?

A

quicksort because the constant is always smaller

23
Q

how to choose the pivot for quicksort?

A

always choose the middle because if you get the first or last you might end up in the worst case scenario because it divides the sorted array into uneven sides and you will often hit the worst case

24
Q

what is a hash function?

A

it takes in a string and gives back a number

25
Q

what are the requirements for a hash function?

A

1 - consistent: meaning apple always returns 1

2 - it should map different words to different numbers as there is no point in returning the name number each time

26
Q

how to avoid collisions in a hash table?

A

1 - low load factor

2 - good hash function

27
Q

what is a load factor in hash tables?

A

number of elements / number of slots

28
Q

how to achieve a lower load factor?

A

by resizing the array of the hash table

29
Q

what is breadth first search?

A

it’s an algorithm used to solve shortest path problems

30
Q

what is a graph?

A

it models a set of connections

31
Q

what are graphs made of?

A

nodes and edges

32
Q

what are neighbors in a graph?

A

a node that is related to another node

33
Q

what are the questions that a BFS answer?

A

1 - is there a connection between Node A and Node B?

2 - what is the shortest path between Node A and Node B?

34
Q

what is the running time of BFS?

A

O(V + E)
v -> number of vertices (nodes)
e -> number of edges

35
Q

what is the difference between directed graph and undirected graph?

A

1 - directed : it follows a relationship from A to B but not from B to A so it follows the direction of an Arrow
2 - undirected : doesn’t have arrows so it goes both ways

36
Q

what does djikstar algorithm find?

A

the fastest path no the shortest like BFS

37
Q

what is the type of graph that dijkstra’s algorithm work with?

A

directed acyclic graph or DAGs

38
Q

what type of weight the dijkstra algorithm cannot work on?

A

negative weighted edges

39
Q

what is a greedy algorithm?

A

it finds the optimal local solution at each step and by that it finds the optimal global solution