CS2040_2020_2021_Sem2_midterm_qns
CS2040_2020_2021_Sem2_midterm_qns
MCQ
This section has 10 questions and is worth 30 marks. 3 marks per question.
a. O(log n)
b. O((log n)2)
c. O(n)
d. O(n log n)
2. The following array is the result of running 2 iterations of a certain sorting algorithm on
an initially unsorted array of integers:
7 9 13 4 18 48 29 67 80
Which of the following algorithms (as taught in lecture) could be the sorting algorithm in
question?
3. The numbers 11, 21, 24, 35, 43, 46 are inserted one at a time into an initially empty hash
table of size 11. The hash function is h(key) = key % 11, and quadratic probing is used as
the collision resolution technique. No deletions are involved, and the exact order in
which the numbers are inserted is unknown. Which of the following hash tables could
be a possible result of these insertions?
0 1 2 3 4 5 6 7 8 9 10
(i) 11 35 24 21 46 43
0 1 2 3 4 5 6 7 8 9 10
(ii) 11 46 43 24 35 21
0 1 2 3 4 5 6 7 8 9 10
(iii) 43 11 24 46 35 21
a. (iii) only
b. (i) and (ii)
c. (ii) and (iii)
d. (i), (ii), and (iii)
4. Given an array containing n integers in descending order, we wish to sort it into
ascending order by using a sorting algorithm as discussed in lecture (as opposed to
simply reversing the array). Choose the best sorting algorithm (in terms of worst case
time complexity) to solve this problem.
a. Insertion sort
b. Improved bubble sort
c. Merge sort
d. Quick sort
5. Which of the following data structures can support all queue operations in O(1) time?
a. (i) only
b. (i) and (iii)
c. (ii) and (iii)
d. (i), (ii) and (iii)
6. You are given a queue of n integers (the value of n is provided to you), and you wish to
reverse the contents of the queue. You are only allowed to use one additional data
structure, though you can declare a few other primitive variables, where necessary.
Additionally, your program should run in less than O(n2) time in the worst case. Which
data structure would be suitable for this task?
i. A stack
ii. Another queue
iii. A HashSet
Note that the options are independent ie. picking (i) and (ii) means it is possible to solve
the problem with either a stack, or another queue.
a. (i) only
b. (ii) only
c. (i) and (iii)
d. (ii) and (iii)
7. We attempt to insert the following keys (in this order) into an initially empty hash table.
The hashing function is h(key) = key % 7, but the collision resolution technique used by
the hash table is unknown. However, it is known that the hash table is of size 7.
What is the load factor of the hash table (rounded to 2 decimal places)?
a. 0.57
b. 0.71
c. 0.86
d. Insufficient information to determine for certain
a. O(log n)
b. O((log n)2)
c. O(n0.5)
d. O(n)
9. You are given an array of sorted distinct integers. You are asked to find the k-th smallest
element in this array. The best algorithm to do so can run in:
a. O(1) time
b. O(log n) time
c. O(n) time
d. O(n log n) time
10. You are given an unknown data structure X, which is initially empty. You are unsure as
to whether it is a stack or a queue, but you are allowed to use the following methods on
X:
insert(int e): adds the integer to the top of the stack (push(e)) if X is a stack, or adds the
integer to the back of the queue (enqueue(e)) if X is a queue.
remove(): removes and returns the top of the stack (pop()) if X is a stack, or removes
and returns the front of the queue (dequeue()) if X is a queue.
Calling any of the above methods counts as 1 operation. How many operations do you
need to determine for sure if X is a stack or a queue?
a. 2
b. 3
c. 4
d. 5
Analysis
This section has 3 questions and is worth 12 marks. 4 marks per question.
Please select True or False and then type in your reasons for your answer.
12. In the separate chaining collision resolution technique, if we bound the load factor to a
constant C (i.e once the load factor exceeds C, we re-hash to a larger hash table to keep
the load factor ≤ C), we can ensure that insertion, deletion and searching in the hash
table can be done in worst case O(1) time.
13. It is possible for a stack implemented using circular linked list with only a tail reference
and no head reference to achieve worst case O(1) for pop() and push(item).
Structured Questions
This section has 5 questions and is worth 58 marks.
Write in pseudo-code.
Any algorithm/data structure/data structure operation not taught in CS2040 must be described,
there must be no black boxes.
Partial marks will be awarded for correct answers not meeting the time complexity required.
Q14 and Q15 are related. Consider them part a and part b of the same question.
14. Implement a queue ADT as efficiently as possible using a stack ADT (you can use multiple
stacks) and some extra variables if necessary.
The only operations you can make use of in for the stack ADT is empty(), push(item), pop().
You may assume all the operations run in O(1) worst case time. The operations you must
implement for the queue ADT are offer(item), poll(). Give the time complexity for your
implementation of each of the queue operations.
[11 marks]
15. Now implement the queue ADT as efficiently as possible using a hashtable (you can only use
1 hashtable) and some extra variables if necessary. For the hashtable, you have the
insert(key,value), retrieve(key) and delete(key) operation which you may assume take
worst case O(1) time. Again the operations you must implement for the queue ADT are
offer(item) and poll(). Give the time complexity for your implementation of each of
the queue operations. [11marks]
16. You are given a linked list of N sorted integers (stored in the val attribute of the node)
where each node at index i (0 ≤ i ≤ N-1) does not only have a next reference to the next
node in the sorted list, but has a reference to the nodes at i+20, i+21, i+22 until i+2k ≤ N-1.
You can assume that for each node at i there is an array R containing all these references
with R[0] containing the reference to node at i+20 , R[1] containing the reference to node at
i+21and so on. There is only a head reference to the first node of this sorted linked list and
no tail reference.
In the above diagram head.val will give you 10 the value stored in the first node. head.R[0] will
access the node containing 23 and head.R[1] will access the node containing 50. For the last
node, R would be null.
Now give an algorithm that return true if a given integer x is in the sorted list else return false.
Your algorithm should run in worst case O(logN) time. [13 marks]
17. The Arithmetica continent is home to a strange kind of creature called numbered animals.
As the name suggests each animals is born with a pattern that resembles a positive integer
and each animal will have a unique number (no repeats) that can be arbitrarily big. There
are in total M (M > 100) number of such creatures.
The animals are organized into 20 domains with each domain being ruled by a queen. The
queen has the largest number among all animals that belong to that domain. As an
example:
If there are only 3 domains, one having a queen with number 50, one having a queen with
number 200 and the last one having a queen with number 300; the one with number 50 can
only rule over at most 49 animals (number 1 to 49) and the one with number 200 can only
rule over at most 149 animals (number 51 to 199), and the one with number 300 can only
rule over at most 99 animals (number 201 to 299).
The domain of the queen with the largest number (which must also be the largest
numbered animal among all the M animals) is considered the 1st domain, the queen with
the 2nd largest number the 2nd domain and so on until the 20th domain.
Now
1.) given array A of size M containing the number of all M numbered animals in no
particular order
2.) given array B of size 20 containing the array index (0-based) of all 20 queens in A, again
in no particular order
Give an algorithm to answer the following query as efficiently as possible and give its time
complexity:
A = {5,100,142,232,11,325,8,21,10000,43}, B = {3,8,1}
If necessary, give an algorithm to perform a preprocessing step before any query comes in.
The preprocessing step must run in worst case ≤ O(M) time [13 marks]
18. The game "Tetritis" is the lastest craze in town.
For each round in this game, you are given N (N ≥ 1) horizontal line segments which are
specified by a triple (x,y,length) where x,y ( 0 ≤ x,y ≤ M-1 and M ≥ NlogN) are integers
indicating the x-coordinate and y-coordinate of the left end of the line segment, and
length is the length of the line segment.
There will be a leftmost line segment whose left end starts from x = 0 and a rightmost
line segment whose right end will end at x = m-1.These N line segments have no
intersection with each other with regards to the x-coordinate they span, but will be
contiguous in terms of the x-coordinate they span (i.e there will be some line segment
occupying each x-coordinate from 0 to M-1).
You can assume the N line segments are stored as the above mentioned triples in an
array H sorted by increasing x-coordinate of their left most end.
Now starting from the y = 0, a horizontal line which I will call the "sticker" spanning x = 0
to M-1 will start moving upwards.
For each line segment encountered, the sticker will "break" with the portion spanning
the line segments encountered being "stuck" to the line segment and the other parts
(sticker segments) will continue moving upwards. So at some point you can have
multiple sticker segments moving upwards. Even if the sticker segment matches the line
segment exactly, it is also considered a "break". Now, regardless of how many line
segments are encountered by a particular sticker segment at a particular y-coordinate, if
there is ≥ 1 line segment that causes a "break" it is considered as 1 "break" not multiple
breaks.
This breaking and moving upwards of the sticker/sticker segments will continue until all
line segments have some sticker segment being stuck to them at which time the round
will end.
The point of the game is for the player to guess how many "breaks" will have happened
when the round ends. The closer to the actual number, the more points.
So given the value of M,N and the array H for a round of the game, give an algorithm that will
output the total number of breaks when the round ends in worst case O(N) time. [10 marks]