Exam 2 Top Hats Flashcards

1
Q

Consider some iterator, iter, which was created on a list of Integers.

Consider the code snippet below:

while (true) {
Integer X = iter.next();
System.out.println(“Next value is “ + X);
}

What will happen if this code segment is executed?

A

All of the values in the list will be printed, followed by an Exception

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

Consider a list, L, containing (in order): 10, 20, 30
Now consider the following code segment:

Iterator<Integer> one = L.iterator();
Iterator<Integer> two = L.iterator();
Integer X = one.next();
Integer Y = two.next();
What are the values X and Y?</Integer></Integer>

A

X = 10, Y = 10

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

What are the the worst-case runtimes of Contains(x), Add(x) and Remove(x) if we implemented Set ADT using an (unsorted) array

A

O(n), O(n), O(n)

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

Which of the following may work as a hash function for strings?
a - Return a random number.
b - Return 0 if the string is of even length and return 1 if it’s of odd length.
c - Add together all the ASCII values of the characters.

A

b,c

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

What are the the worst-case runtimes of Contains(x), Add(x) and Remove(x) if we implemented Set ADT using a sorted array?

A

O(log n), O(n), O(n)

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

Which tasks can be efficiently solved using the Hash Table implementation of Set ADT?
a - Removing duplicates from the array of integers
b - Quickly checking if student name is in the class roster
c - Testing if all the elements of a given array are unique
d - Given an array of people’s names and a given family name s - counting how many times s appears in the array.

A

a,b,c

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

Algorithm findArrayMax(A, n):
input: a NONEMPTY array, A with n elements
output: A’s maximum element

Recursive array max: stop condition?

A

if n == 1 return A​[0]

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

Algorithm findArrayMax(A, n):
input: a NONEMPTY array, A with n elements
output: A’s maximum element

if n == 1:
return A​[0]
*

Recursive array max: recursive step *

A

if A​[n-2] < A​[n-1]:
A ​[n-2] = A​[n-1]
return findArrayMax(A, n-1)
else:
return findArrayMax(A, n-1)

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

You are implementing algorithm power(x,y) for computing xy.

What is your base case?
y = 1 and 0

A

If y == 1, return x
If y == 0, return 1

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

You are implementing algorithm power(x,y) for computing xy. (x and y are positive integers)

What is your recursive step?

A

power(x,y) = x*power(x,y-1)

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

Consider the recursive algorithms for computing factorial and for a search in an unsorted array (sequential search).

Which of the following claims are true?
a - The recursive versions of these algorithms are preferable to the iterative versions because the recursive versions are asymptotically faster.
b - The recursive versions of these algorithms are preferable to the iterative versions because the recursive versions have less overhead in their execution.
c - The iterative versions of these algorithms are preferable to the recursive versions because the iterative versions are asymptotically faster.
d - The iterative versions of these algorithms are preferable to the recursive versions because the iterative versions have less overhead.

A

d

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

Here is an algorithm for counting the number of times the item is found in a sorted array A:

pos = binarySearch(A, item)
if pos!== -1:
return 0
count = 1

i = pos+1
while (i < A.length):
if (A​[i] == item):
count++
i++
else: break

i = pos-1
while (i >=0):
if (A​[i] == item):
count++
i–
else: break
What is the time complexity of this algorithm?

A

O(n)

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

Consider the following code:

displayNode (Node current) {
if (current != null) {
print (current.data)
displayNode (current.next)
}
}

Let head be a head of a Linked List: 1->2->3->4

What will be printed after we call

displayNode (head)

A

1 2 3 4

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

Consider the following recursive step for the divide and conquer power function, pow(N):

int recAns = pow(x,N/2);
return (recAns*recAns);
When is the recursive case correct?

A

This recursive case is correct for some values of N

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

Consider the “sorted “ vs. the “unsorted partition” in the array in the middle of Insertion Sort. Which statements about the elements in the sorted partition are true?
a - The data in a sorted partition is sorted and each item at location k will stay at location k for the rest of the sort.
b - The data is sorted but the position of an element currently at location k can still change.
c - The data is not actually sorted yet and will become sorted only after the last iteration.

A

The data is sorted but the position of an element currently at location k can still change.

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

Consider the last iteration of the outer loop of Insertion Sort. How many comparisons must be done for this iteration?

A

It could be from 1 to N-1 comparisons, depending upon where the item will end up in the array.

17
Q

If we perform Insertion Sort and the elements we are sorting are stored in a Linked List: which input will give the best-case performance, and which one the worst-case performance?

A

Best-case: when the input is sorted in reverse, worst-case: when the input is sorted ascending.

18
Q

What is the main difference between a shell sort and the sort that it is a generalization of?

A

A shell sort takes bigger “steps”

19
Q

If the input of the algorithm is already sorted, which of the following sorting algorithms will run in time O(n)?

A

Bubble Sort, Insertion Sort

20
Q

Consider the simple Quick Sort algorithm discussed in lecture (leftmost element chosen as the pivot). If the input data is sorted prior to the call of Quick Sort – which of the statements below are true?
a - The algorithm will run in time O(N2). This will be the worst-case input for the algorithm
b - The algorithm will run in time O(N log N).
c - The algorithm will run in time O(N): this is the best-case input for this algorithm.

A

a

21
Q

We know that with the simple pivot (leftmost item), a worst case is when the input data is already sorted prior to the algorithm. If rather than the leftmost item for the pivot, we instead use the rightmost item as the pivot, how will the Quick Sort run-time be affected?

A

It will still be the worst-case of the Quick Sort algorithm.

22
Q

What are the values of the overlap function for each position in P=”AAAB”?

A

0 1 2 0

23
Q

Given pattern:
ABCABCXB

What are the values of the overlap function for positions 5, 6 and 7.

A B C A B C X B
0 1 2 3 4 5 6 7
0 0 0 1 2

A

3,0,0

24
Q

Here is a snapshot after first iteration of the KMP algorithm. We matched characters from 0 to 6 and now we have a mismatch.

What is the start of the next alignment in T and which characters of pattern P and text T will be compared next according to the KMP algorithm?

A

We start with T​[7] and compare T​[7] and P​[0]

25
Q

In the Las Vegas version of the Rabin-Karp string matching algorithm (when hashes match, check the characters), which are true? (Note: select all that apply)
a - It is guaranteed to be correct)
b - It is guaranteed to have a run-time of O(N)
c - It is very likely to be correct
d - It is very likely to have a run-time of O(N)
e - It is guaranteed to have a run-time of O(MN)

A

a,d,e