Quiz 1
Quiz 1
Quiz 1
• Do not open this quiz booklet until directed to do so. Read all the instructions on this page.
• When the quiz begins, write your name on the top of every page of this quiz booklet.
• You have 120 minutes to earn a maximum of 120 points. Do not spend too much time on
any one problem. Skim them all first, and attack them in the order that allows you to make
the most progress.
• You are allowed one double-sided letter-sized sheet with your own notes. No calculators,
cell phones, or other programmable or communication devices are permitted.
• Write your solutions in the space provided. Pages will be scanned and separated for grading.
If you need more space, write “Continued on S1” (or S2, S3, S4) and continue your solution
on the referenced scratch page at the end of the exam.
• Do not waste time and paper rederiving facts that we have studied in lecture, recitation, or
problem sets. Simply cite them.
• When writing an algorithm, a clear description in English will suffice. Pseudo-code is not
required. Be sure to argue that your algorithm is correct, and analyze the asymptotic
running time of your algorithm. Even if your algorithm does not meet a requested bound,
you may receive partial credit for inefficient solutions that are correct.
• Pay close attention to the instructions for each problem. Depending on the problem,
partial credit may be awarded for incomplete answers.
Name:
School Email:
2 6.006 Quiz 1 Name
(a) [1 point] Write your name and email address on the cover page.
(a) [5 points] Given array A of n integers, the Python function below appends all integers
from set {A[x] | 0 ≤ i ≤ x < j ≤ n and A[x] < k} to the end of dynamic array B.
1 def filter_below(A, k, i, j, B):
2 if (j - i) > 1:
3 c = (i + j) // 2
4 filter_below(A, k, i, c, B)
5 filter_below(A, k, c, j, B)
6 elif (j - i) == 1 and A[i] < k:
7 B.append(A[i])
Argue the worst-case running time of filter below(A, k, 0, len(A), [])
in terms of n = len(A). You may assume that n is a power of two.
(b) [5 points] The integer array A = [4, 3, 1, 5, 0, 2] is not a heap. It is possible to make
A either a max or min heap by swapping two integers. State two such integers.
4 6.006 Quiz 1 Name
(c) [5 points] Let T be a binary search tree storing n integer keys in which the key k
appears m > 1 times. Let p be the lowest common ancestor of all nodes in T which
contain key k. Prove that p also contains key k.
(d) [5 points] Given the hash family H = {ha (k) = a(k + a) mod m | a ∈ {1, . . . , m}}
and some key k1 ∈ {0, . . . u − 1} where 2 < 2m < u, find a key k2 ∈ {0, . . . , u − 1}
with k2 6= k1 such that ha (k1 ) = ha (k2 ) for every ha ∈ H.
6.006 Quiz 1 Name 5
(a) [6 points] A dealer in the casino has a deck of cards that is missing 3 cards. He will
help Jane find Dr. Yes if she helps him determine which cards are missing from his
deck. A full deck of cards contains kn cards, where each card has a value (an integer
i ∈ {1, . . . , n}) and a suit (one of k known English words), and no two cards have
both the same value and the same suit. Describe an efficient1 algorithm to determine
the value and suit of each of the 3 cards missing from the deck.
1
By “efficient”, we mean that faster correct algorithms will receive more points than slower ones.
6 6.006 Quiz 1 Name
(b) [6 points] The dealer doesn’t know Dr. Yes, but he knows that Dr. Yes is one of the
k best players in the casino. Jane scans the room and for each of the p > k players,
she transmits back to headquarters a pair (c, `) representing the number of chips c
and location ` of the player. Assuming that no player has the same number of chips,
describe an efficient algorithm for headquarters to determine the locations of the k
players in the casino who have the most chips.
6.006 Quiz 1 Name 7
(c) [6 points] After determining the locations of the k players with the most chips, Jane
observes the game play of each of them. She watches each player play exactly h < k
game rounds. In any game round, a player will either win or lose chips. A player’s
win ratio is one plus the number of wins divided by one plus the number of losses
during the h observed hands. Given the number of observed wins and losses from
each of the k players, describe an efficient algorithm to sort the players by win ratio.
8 6.006 Quiz 1 Name
(a) [10 points] Assuming b2 − b1 < 6006, describe an O(n)-time algorithm to return
a range pair of A with respect to range (b1 , b2 ) if one exists. State whether your
algorithm’s running time is expected, worst-case, and/or amortized.
10 6.006 Quiz 1 Name
(b) [15 points] Assuming logn (max A − min A) < 6006 (with no restriction on b1 or
b2 ), describe an O(n)-time algorithm to return a range pair of A with respect to range
(b1 , b2 ) if one exists. State whether your algorithm’s running time is expected, worst-
case, and/or amortized.
6.006 Quiz 1 Name 11
For example, the left smaller count array of A = [10, 5, 12, 1, 11] is S = [0, 0, 2, 0, 3]. Describe an
O(n log n)-time algorithm to compute the left smaller count array of an array of n distinct integers.
State whether your algorithm’s running time is worst-case, amortized, and/or expected.
6.006 Quiz 1 Name 13
You can use this paper to write a longer solution if you run out of space, but be sure to write
“Continued on S1” on the problem statement’s page.
14 6.006 Quiz 1 Name
You can use this paper to write a longer solution if you run out of space, but be sure to write
“Continued on S2” on the problem statement’s page.
6.006 Quiz 1 Name 15
You can use this paper to write a longer solution if you run out of space, but be sure to write
“Continued on S3” on the problem statement’s page.
16 6.006 Quiz 1 Name
You can use this paper to write a longer solution if you run out of space, but be sure to write
“Continued on S4” on the problem statement’s page.