Everything Flashcards

1
Q

What is an algorithm?

A

a set of instructions for accomplishing a task

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

True or False? For binary search to work, its elements have to be sorted.

A

True

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

State briefly how the binary search works.

A

With binary search, you guess the middle number and eliminate half the remaining numbers every time.

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

Suppose you’re looking for a word in the dictionary. The dictionary has 240,000 words. In the worst case, how many steps do you think each search will take?

Simple Search?
Binary Search?

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

In general, for any list of size n, binary search will take ______ steps to run in the worst case, whereas simple search will take __ steps.

A

log2n

n

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

Binary search runs in _______ time

A

logarithmic

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

What’s the Big O for a linear search?

A

O(n)

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

What’s the Big O for a binary search?

A

O(logn)

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

What does the Big O notation tell you?

A

How fast an algorithm is. The worst case.

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

True or false? Big O tells you how fast the algorithm grows.

A

True

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

Big O establishes a _____-case runtime

A

worst

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

How does a selection sort algorithm work?

A

The selection sort algorithm sorts an array by repeatedly finding the minimum element (considering ascending order) from unsorted part and putting it at the beginning.

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

What’s the Big O of a selection sort algorithm?

A

O(n2)

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

What’s the Big O of the Quicksort Algorithm?

A

O(nlogn)

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

If you want to insert an element into the middle of a list, what’s better to use, arrays or linked lists?

A

Linked lists, since in arrays you would have to shift everything down.

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

What if you want to delete an element from a list? Which is better? Array or Linked List.

A

Linked List. With arrays, you would have to shift all of the elements.

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

When searching for elements, what are the two types of access?

A

Random access and Sequential Access

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

_______ ________ means reading elements one by one, starting at the first element.

A

Sequential Access

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

True or False? Linked lists can do random access or sequential access.

A

False. Only sequential access

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

_______ _______ means you can jump directly to the 10th element.

A

Random Access

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

True or false? Arrays are faster at reads than linked lists

A

True. Arrays allow for random access while linked lists only allow for sequential access

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

True or false? There is a performance benefit to using recursion

A

False.

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

Every recursive function has two parts. What are they?

A

The base case and the recursive case.

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

True or false? Quicksort is faster than selection sort

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
What is the Big O for quicksort?
O(nlogn)
26
Selection sort has a Big O of \_\_\_\_\_.
O(n2)
27
Generally speaking, how does Divide and Conquer work?
By breaking a problem down into smaller and smaller pieces.
28
If you’re using D&C on a list, the base case is probably an \_\_\_\_\_ array or an array with ____ element.
empty one
29
What is a hash function?
A hash function is a function where you put in a string and you get back a number.
30
In technical terminology, we’d say that a hash function “\_\_\_\_\_\_\_\_\_\_ strings to numbers.”
maps
31
If you feed an "apple" into the hash function, and you get back the number 3, what is the 3?
It can be the index location where you stored price of an apple.
32
True or False? A hash function needs to be consistent. For example, suppose you put in “apple” and get back “4”. Every time you put in “apple”, you should get “4” back.
True. Without this, your hash table won’t work.
33
True or false? The hash function knows how big your array is and only returns valid indexes. So if your array is 5 items, the hash function doesn’t return 100.
True
34
Put a hash function and an array together, and you get a data structure called a _______ \_\_\_\_\_\_\_.
hash table
35
True or false? Hash tables are sometimes referred to as hash maps, maps, dictionaries, and associative arrays.
True
36
Python has hash tables; they’re called \_\_\_\_\_\_\_\_\_\_.
dictionaries
37
A hash table maps _____ to \_\_\_\_\_\_.
keys to values
38
What are collisions in hash tables?
Hash functions don't always give you a unique number when they're fed a string. Sometimes they give the same number to two different strings. That can overwrite elements in an array. That's called a collision: when two keys have been assigned to the same slot.
39
What's a simple way to avoid collisions?
Start a linked list at that slot
40
What's the Big O for searching hash tables?
O(1)
41
O(1) is called ______ time.
constant
42
True or false? Constant time means instant.
False. It means that the time will stay the same regardless of how long, for example, a hash table is.
43
True or false? Tt doesn’t matter whether your hash table has 1 element or 1 billion elements—getting something out of a hash table will take the same amount of time.
True
44
True or false? Because many programs use it as an intermediate step, sorting is a fundamental operation in computer science.
True
45
An algorithm is said to be ______ if, for every input instance, it halts with the correct output.
correct
46
We say that a correct algorithm ______ the given computational problem.
solves
47
A ______ \_\_\_\_\_\_\_\_ is a way to store and organize data in order to facilitate access and modifications.
data structure
48
What type of algorithm is this?
Insertion sort
49
\_\_\_\_\_\_\_\_\_ an algorithm has come to mean predicting the resources that the algorithm requires.
Analyzing
50
True or false? Insertion Sort can take different amounts of time to sort two input sequences of the same size depending on how nearly sorted they already are.
True
51
The _______ \_\_\_\_\_\_ of an algorithm on a particular input is the number of primitive operations or “steps” executed.
running time
52
The running time of the algorithm is the ___ of running times for each statement executed
sum
53
In insertion sort, if a list is already sort in ascending order we get the ____ runtime result and the _____ case if the list is sorted in descending order.
best worst
54
The worst-case running time of an algorithm gives us an _____ bound on the running time for any input.
upper
55
Recursive algorithms typically follow a ____ \_\_\_ _______ approach
divide-and-conquer
56
What do divide and conquer algorithms do?
they break the problem into several subproblems that are similar to the original problem but smaller in size, solve the subproblems recursively, and then combine these solutions to create a solution to the original problem.
57
How does merge sort work?
Divide: Divide the n-element sequence to be sorted into two subsequences of n/2 elements each. Conquer: Sort the two subsequences recursively using merge sort. Combine: Merge the two sorted subsequences to produce the sorted answer.
58
In merge sort, recursion “bottoms out” when the sequence to be sorted has length \_\_\_, in which case there is no work to be done, since every sequence of length ____ is already in sorted order.
1 1
59
Briefly illustrate how you would merge the elements in a merge sort.
60
In computer science, ______ \_\_\_\_ ______ is an algorithm design paradigm based on multi-branched recursion
divide and conquer
61
How does the divide and conquer algorithm work?
A divide-and-conquer algorithm works by recursively breaking down a problem into two or more sub-problems of the same or related type, until these become simple enough to be solved directly. The solutions to the sub-problems are then combined to give a solution to the original problem.
62
True or false? The divide-and-conquer paradigm often helps in the discovery of efficient algorithms.
True
63
Divide-and-conquer algorithms are naturally implemented as ________ procedures.
recursive
64
Describe how the binary search algorithm works.
In each step, the algorithm compares the input element x with the value of the middle element in array. If the values match, return the index of middle. Otherwise, if x is less than the middle element, then the algorithm recurs for left side of middle element, else recurs for right side of middle element.
65