Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

Question 1

 Bookmark this page

Question 1
3.0/3.0 points (ungraded)
1. Which one of these relations on strings is not a total ordering?

A ≤ B if the length of A is not greater than the length of B


A ≤ B if the length of A is not less than the length of B
A ≤ B if B is a prefix of A correct
A ≤ B if B contains at least as much whitespaces as A
A ≤ B if A appears not later than B in the «Ulysses» novel of James Joyce
(assuming that all strings not appearing in this novel are equal and greater
than all strings which do appear)
Let me explain:
Being a prefix is not a total ordering relation, because two strings «a» and «b»
are not prefixes of each other — so it is not true that either «a» ≤ «b» or «b» ≤
«a».
2. Which is the fastest way to find the minimum of the given arbitrary sequence?

Sort this sequence, and return the first element


Put the sequence into a deque, and return the first element
Check all the elements of the sequence, while tracking the minimum
element seen so far correct
Apply the binary search to the sequence
Let me explain:
Checking all the elements would consider every element exactly once. To sort
the sequence, you will perform more operations in any case. Using a deque
does not solve this problem at all, and binary search cannot be applied to a
sequence which is not sorted.
3. For a large library, what is the best way, out of the given ones, to organize
storage for fast access to the book by its name (assuming names are in
English)?

Store all the books sorted in alphabetical order in several bookcases,


where bookcases are themselves sorted.
Keep books in whatever order, but have small cards which show the name
of the book and its location, and sort all these cards in alphabetical order.
Keep books in whatever order, but have small cards which show the name
of the book and its location, and sort all these cards grouped by the first letter
of the book's name, and sorted in alphabetical order in each of these
groups. correct
Keep books in whatever order, but have small cards which show the name
of the book and its location, and sort all these cards grouped by the book size,
in pages, and sorted in alphabetical order in each of these groups.
Let me explain:
In a large library, the computational complexity of querying which book is in
bookcase X on shelf Y at position Z is itself quite large, because you have to
move physically a lot! So having cards is more practical, as all operations with
these cards are «local».
Among all the mentioned ways of working with the cards, the fastest is to
group them by the first letter — because it takes almost the same time for a
human to pick up a shelf with a given letter, and to check what's in the middle
of a certain interval of cards. Grouping by the first letter saves three to four
lookups for every query. In contrast, grouping by book size makes things
much worse, because you typically don't know the book size in advance.
Question 2
 Bookmark this page

Question 2
1.0/2.0 points (ungraded)
1. Assuming you sort an array of size 25 with an insertion sort. How many
comparisons can you perform, assuming that the insertion sort is written
optimally? Check all possible answers.

20
50 correct
100 correct
200 correct
300 correct
500
1000

incorrect
Let me explain:
For a size N, you cannot do fewer than N-1 comparisons. The maximum
number of comparisons for the insertion sort is N (N-1) / 2, which is 300 for
N=25.
2. Consider an array of integers, which has a size of 2N, where the element at
index X (0 ≤ X < 2N) is X xor 3 (that is, two least significant bits of X are
flipped). What is the running time of the insertion sort on this array?
Θ(2N) correct
Θ(N2N)
Θ(N22N)
Let me explain:
The only inversions in this array are limited to groups of elements at indices
4T, 4T+1, 4T+2, 4T+3 for all integer T. The number of inversions for each
such group is at most constant (six, to be precise), and the number of groups
is linear in the size of the array, so the overall running time of the insertion
sort is linear as well.
Question 3
 Bookmark this page

Question 3
2.0/2.0 points (ungraded)
1. Which of the following is true?

To solve the scalar maximization problem, one shall use only sorting
algorithms which compare and swap adjacent elements, such as the insertion
sort.
To solve the scalar maximization problem, one shall use only sorting
algorithms which are designed specifically to sort integers.
To solve the scalar maximization problem, one may use any possible
sorting algorithm. correct
Let me explain:
Although our proof of optimality of sorted sequences as a solution to the
scalar maximization problem is based on comparing and swapping only
adjacent elements, the results of all sorting algorithms are indistinguishable
from the point of view of this problem. So, all sorting algorithms can be used.
2. Which of the following is true?

The proof of the optimality of sortedness for the scalar maximization


problem has a problem with negative numbers, because the product of two
negative numbers is positive (and can be very large).
The proof of the optimality of sortedness for the scalar maximization
problem has no problems with negative numbers. correct
Let me explain:
This proof never uses the assumption that the numbers are non-negative —
for instance, it never divides one number by another one. It also works with
zeros for the same reason. However, be aware that in your particular problem
this may or may not be true — depending on what you have to prove!
Question 4
 Bookmark this page

Question 4
0.0/1.0 point (ungraded)

In which of the following cases the array is correctly split into two parts with respect
to the given pivot element?

[1, 10, 25, 100, 25, 154], pivot = 1 correct

[1, 10, 25, 100, 25, 154], pivot = 10 correct

[1, 10, 25, 100, 25, 154], pivot = the first 25 correct

[1, 10, 25, 100, 25, 154], pivot = 100

[1, 10, 25, 100, 25, 154], pivot = the second 25

[1, 10, 25, 100, 25, 154], pivot = 154 correct


incorrect

Let me explain:
In the correct cases any element to the left of the pivot is less or equal to the pivot,
and any element to the right of the pivot is greater or equal to the pivot

Question 5
 Bookmark this page

Question 5
0.0/1.0 point (ungraded)

Which of the following strategies of choosing the pivot may make Quicksort always
running in Θ(N2) time on some arrays? (The variables s and e are the boundaries of
the part of the array which is being sorted.)
s correct

e correct

random pivot

(s+e)/2 correct
incorrect

Let me explain:
Choosing either s or e leads to quadratic runtime on sorted arrays. There also exists
a bad case for the (s+e)/2 pivot (see Lecture 4). On the contrast to these, choosing a
random pivot leads to expected O(N log N) runtime on any input. or N=25.

Question 6
 Bookmark this page

Question 6
1.0/2.0 points (ungraded)
1. Assume that the subarrays L=[2, 10, 25] and R=[1, 14, 18] are merged during
Mergesort. Choose the resulting array.

[1, 14, 18, 2, 10, 25]


[2, 10, 25, 1, 14, 18]
[1, 2, 10, 14, 18, 25] correct
[2, 1, 10, 14, 25, 18]
[1, 2, 14, 10, 18, 25]
Let me explain:
The resulting array should be sorted.
2. Select the correct statement about the merge procedure. N is the total number
of elements in the subarrays being merged. (Notice that these statements
consider only the procedure of merging two subarrays, not the whole
Mergesort algorithm!)

The procedure correctly merges any subarrays into a sorted array in O(N)
The procedure correctly merges sorted subarrays into a sorted array in
O(N) correct
The procedure correctly merges any subarrays into a sorted array in ϴ(N
log N)
The procedure correctly merges sorted subarrays into a sorted array in
ϴ(N log N) incorrect
Let me explain:
To get a correctly sorted array using the merge procedure, the input
subarrays should be already sorted. The merge procedure writes every
element to the scratch array exactly once, so its runtime is O(N).

You might also like