CS2040 1920SEM1 Midterm
CS2040 1920SEM1 Midterm
SCHOOL OF COMPUTING
Midterm (20%)
AY2019/20 Semester 1
INSTRUCTIONS TO CANDIDATES
1. Do NOT open the question paper until you are told to do so.
2. This question paper contains TWO (2) sections with sub-questions. Each section has a
different length and different number of sub-questions. It comprises Thirteen (13) printed
pages, including this page.
3. Answer all questions in this paper itself. You can use either pen or pencil. Write legibly!
4. This is an Open Book Quiz. You can check the lecture notes, tutorial files, problem set files,
CP3 book, or any other books that you think will be useful. But remember that the more
time that you spend flipping through your files implies that you have less time to actually
answer the questions.
5. When this Quiz starts, please immediately write your Matriculation Number and Tutorial
Group (if you don’t know your tutorial group, write your TA name and time slot).
A
STUDENT NUMBER:
Total 100
- 1 of 13 -
CS2040 Midterm (AY2019/20 Semester 1)
2. Given the same input, insertion sort will always have time complexity equal to or worse
than mergesort. [true/false]
- 2 of 13 -
CS2040 Midterm (AY2019/20 Semester 1)
3. If we implement a queue ADT using a circular linked list, only the time complexity for
either enqueue or dequeue operation is 𝑂𝑂(1) time but not both. [true/false]
4. Given 2 arrays of size 𝑁𝑁 each that contains unsorted integers that have values between
0 and 2𝑁𝑁 , we can check whether they contain the same elements in 𝑂𝑂(𝑁𝑁) time by first
performing radix sort on both in 𝑂𝑂(𝑁𝑁) time then simply go from index 0 to 𝑁𝑁-1 to check
if the value at the current index in both array is the same or not. [true/false]
- 3 of 13 -
CS2040 Midterm (AY2019/20 Semester 1)
a) After taking CS2040 for half a semester, you have decided to put what you’ve learned to
good use and simulate a rudimentary text editor. To make it simple you only capture input
from the keys ‘0’ to ‘9’, ‘a’ to ‘z’, ‘A’ to ‘Z’, the space key ‘ ’, the enter key ‘\n’ and the
backspace key which is captured as ‘\b’. These inputs are then stored as a string. Given
such a length 𝑁𝑁 input string 𝑨𝑨, give an algorithm and the associated data structure to
display the final text in 𝑂𝑂(𝑁𝑁) time. [12 marks]
b) After you tried out your simulator, you discovered that somehow the backspace key press
has a problem and instead of a backspace it will randomly be replaced by a home key or
end key. Going with the error, you now want to change your algorithm and associated
data structure so that it will output the final text with the inclusion of these random home
and end keys and the exclusion of the backspace key. You can assume home key is ‘\H’
and end key is ‘\E’. Your algorithm should still run in 𝑂𝑂(𝑁𝑁) time. [18 marks]
- 4 of 13 -
CS2040 Midterm (AY2019/20 Semester 1)
- 5 of 13 -
CS2040 Midterm (AY2019/20 Semester 1)
A facility manufactures a type of poisonous liquid. It will then bottle those liquid into bottles
that can contain different integer volume 𝑀𝑀 of liquid where 1 𝑙𝑙𝑙𝑙𝑙𝑙𝑙𝑙𝑙𝑙 < 𝑀𝑀 < 10 𝑙𝑙𝑙𝑙𝑙𝑙𝑙𝑙𝑙𝑙.
Batches of 𝑁𝑁 bottles will be sent on a conveyor belt one after the other to be filled with the
poisonous liquid. The bottle at the front will be processed first while the bottle at the back
will be processed last. The bottle at the front will then be filled with up to 3 litres of liquid or
until the bottle is full whichever is less, and it takes 10 seconds per litre. If the bottle is not
full it will be sent to the back of the conveyer belt to be filled again (assume no time is taken
here). After processing a bottle (whether it is full or not), the next bottle takes 10 seconds to
come in and be positioned correctly. You may assume that at the start the 1st bottle to be
processed is already positioned correctly.
a.) Given an array 𝐴𝐴 of 𝑁𝑁 (𝑁𝑁 > 1) integer values indicating the volume of 𝑁𝑁 bottles to be placed
on the conveyer belt from index 0 to index 𝑁𝑁 − 1 in that order, and 𝐻𝐻 the number of seconds,
give the algorithm and associated data structure that will compute the number of bottles that
are completely full after 𝐻𝐻 seconds. [12 marks]
E.g1: Given 𝐴𝐴 = [1,3,7,1,4] and 𝐻𝐻 = 190, the number of full bottles is 3 after 190 seconds.
E.g2: Given 𝐴𝐴 = [1,3,1,4,7] and 𝐻𝐻 = 190, the number of full bottles is 4 after 190 seconds.
- 6 of 13 -
CS2040 Midterm (AY2019/20 Semester 1)
This facility is a single floor rectangular structure of width 100 meters with strangely no roof
and running along the 2 sides of the facility length-wise are walls of 𝑴𝑴 + 𝟏𝟏 meters high each
where 𝑴𝑴 is an integer. Along the length of the 2 side walls, 𝑵𝑵 walls of integer heights ranging
from 3 meters to 𝑴𝑴 meters are placed at intervals of 10 meters perpendicular to the side
walls to create rooms and since the pipes run through all the rooms, they all start filling with
the poisonous liquid. Once the liquid in a room reaches over the height of either of its room
walls, it will overflow into the adjoining room and so on, until it flows into the moat. You can
assume the moat can contain and neutralize an infinite amount of the liquid. Your task is to
now calculate what is the maximum volume of liquid contained within the facility. For
simplicity sake assume walls are 0 meters thick and ignore all the equipment in the facility.
When the facility contains the maximum volume of liquid, the liquid will fill the facility such
that for all pairs of walls 𝑊𝑊 and 𝑊𝑊’ (assuming without loss of generality that 𝑊𝑊 is to the left
of 𝑊𝑊’) of height ℎ and ℎ’ respectively such that
1. there are no walls taller than min(ℎ, ℎ′ ) between 𝑊𝑊 and 𝑊𝑊’
2. If ℎ ≤ ℎ′ (𝑊𝑊 is the shorter wall), there are no walls taller than ℎ to the left of 𝑊𝑊
3. If ℎ′ ≤ ℎ (𝑊𝑊’ is the shorter wall), there are no walls taller than ℎ′ to the right of 𝑊𝑊′
then all rooms between 𝑊𝑊 and 𝑊𝑊′ will be submerged up to height min(ℎ, ℎ′ ).
The input given to you is an array 𝑨𝑨 of the height of the walls separating the rooms starting
from the 1st wall to the 𝑁𝑁 𝑡𝑡ℎ wall.
100m width
6th wall
Walls separating
rooms at 10m
1st wall
The 2 side walls are height 𝑀𝑀+1 each while the 6 walls have integer height ranging from 3 to 𝑀𝑀.
- 7 of 13 -
CS2040 Midterm (AY2019/20 Semester 1)
13m
11m
7m 8m
6m 6m
The heights of the walls from the 1st to the 6th is 11,13,7,6,8,6 meters respectively.
In the example above, the maximum volume of poisonous liquid contained in the facility can
be shown below based on how the liquid submerges the rooms.
13m
11m
7m 8m
6m 6m
(11*10*100)+(8*30*100)+(6*10*100) = 41,000 m3
Any excess liquid will simply flow into the moat (since there is no roof).
11m 10m
8m
6m 5m
4m
the maximum volume of poisonous liquid contained in the facility can be shown as follows
11m 10m
8m
6m 5m
4m
10*10*100+8*10*100+6*10*100+5*10*100+4*10*100 = 33,000 m3
- 8 of 13 -
CS2040 Midterm (AY2019/20 Semester 1)
b) Given input array 𝑨𝑨 of the height of the 𝑁𝑁 walls from 1st wall to 𝑁𝑁 𝑡𝑡ℎ wall, come up with
an algorithm and appropriate data structure so that the maximum volume of poisonous
liquid that can be contained in the facility can be computed in 𝑂𝑂(𝑁𝑁) time. [18 marks]
- 9 of 13 -
CS2040 Midterm (AY2019/20 Semester 1)
a) Company W houses some of the most advanced military weapons in the world. Each floor
of the company is laid out in an 𝑁𝑁 by 𝑀𝑀 grid (𝑁𝑁 is the # of rows and 𝑀𝑀 is the # of columns),
where the top left cell is at row 0, column 0, i.e (0,0) and the bottom right cell is at row
𝑁𝑁 − 1 and column 𝑀𝑀 − 1, i.e (𝑁𝑁 − 1,𝑀𝑀 − 1). In order to protect their assets, 𝐾𝐾 (𝑘𝑘 ≤
min(𝑀𝑀, 𝑁𝑁) number of laser defenses are installed on every floor. Each laser defenses’
position is given by its row and column coordinate on the grid. Now a laser defense at
coordinate (X,Y) will have a laser barrier shooting out all the way along both row X and
column Y. Due to this feature of the laser defense, the following non-conflict property
must hold:
No laser defense can be placed on the same row or column as another laser defense
otherwise their laser barrier will destroy each other.
E.g: Given the 2 following grid and laser defenses, Fig 1 is an invalid configuration while
Fig 2 is a valid configuration.
(1,0)
(1,0) (1,2)
(2,1)
(3,1)
(4,3)
(4,3)
Fig1: 5x4 grid, with laser Fig2: 6x4 grid, with laser
defenses as indicated defenses as indicated
by their coordinates. by their coordinates.
Lasers at (1,0) & (1,2) No lasers violate the
violates the non-conflict non-conflict property
property here.
You may assume there is a coordinate class with attributes 𝑥𝑥 and 𝑦𝑦 representing the row
and column index respectively.
Now given 𝑁𝑁, 𝑀𝑀, 𝐾𝐾 and an array 𝐴𝐴 of 𝐾𝐾 coordinates representing the position of the 𝐾𝐾
lasers, give an algorithm for the method Valid(𝑵𝑵, 𝑴𝑴, 𝑲𝑲, 𝑨𝑨) that returns true if the 𝐾𝐾 lasers
are placed in a non-conflicting configuration otherwise return false. Valid(𝑵𝑵, 𝑴𝑴, 𝑲𝑲, 𝑨𝑨)
must run in 𝑂𝑂(𝐾𝐾) time. [10 marks]
- 10 of 13 -
CS2040 Midterm (AY2019/20 Semester 1)
- 11 of 13 -
CS2040 Midterm (AY2019/20 Semester 1)
E.g: In the 2 following grid, where # marks the position of the intruder, the bold and
underlined laser configuration will be activated, and its rectangular region are the shaded
cells.
Now given 𝑁𝑁, 𝑀𝑀, 𝐾𝐾, 𝐴𝐴 as before and the coordinates 𝑃𝑃 of an intruder, give the best
algorithm you can think of for the method SmallestArea(𝑵𝑵, 𝑴𝑴, 𝑲𝑲, 𝑨𝑨, 𝑷𝑷) which will return
the area of the activated rectangular laser configuration that will trap the intruder. If
there is no laser configuration that can trap the intruder return -1. Analysis the time
complexity of your algorithm. [18 marks]
- 12 of 13 -
CS2040 Midterm (AY2019/20 Semester 1)
== END OF PAPER ==
- 13 of 13 -