0% found this document useful (0 votes)
7 views13 pages

CS2040 1920SEM1 Midterm

This document outlines the midterm examination for CS2040 - Data Structures and Algorithms at the National University of Singapore, detailing instructions, sections, and questions. It consists of two sections: Section A focuses on analysis with true/false statements requiring proof or counterexamples, while Section B involves applications requiring algorithms in pseudo-code related to text manipulation and a poison manufacturing facility. The total marks for the exam is 100, with specific marks allocated to each question.

Uploaded by

Reynard Ardian
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views13 pages

CS2040 1920SEM1 Midterm

This document outlines the midterm examination for CS2040 - Data Structures and Algorithms at the National University of Singapore, detailing instructions, sections, and questions. It consists of two sections: Section A focuses on analysis with true/false statements requiring proof or counterexamples, while Section B involves applications requiring algorithms in pseudo-code related to text manipulation and a poison manufacturing facility. The total marks for the exam is 100, with specific marks allocated to each question.

Uploaded by

Reynard Ardian
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

NATIONAL UNIVERSITY OF SINGAPORE

SCHOOL OF COMPUTING
Midterm (20%)
AY2019/20 Semester 1

CS2040 – Data Structures and Algorithms

5 October 2019 Time allowed: 90 minutes

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).

6. The total marks for this paper is 100.


TUTORIAL GROUP

A
STUDENT NUMBER:

For examiners’ use only


Question Max Marks
Q1-4 12
Q5 30
Q6 30
Q7 28

Total 100

- 1 of 13 -
CS2040 Midterm (AY2019/20 Semester 1)

Section A – Analysis (12 Marks)


Prove (the statement is correct) or disprove (the statement is wrong) the following
statements below. If you want to prove it, provide the proof or at least a convincing
argument. If you want to disprove it, provide at least one counter example. 3 marks per each
statement below (1 mark for circling true or false, 2 marks for explanation):

1. The tightest time complexity of recursion1(𝑵𝑵) of following program is 𝑂𝑂(𝑁𝑁𝑁𝑁𝑁𝑁𝑁𝑁).


[true/false]
public static void recursion1(int i) {
if (i <= 1)
return;
else
for (int j=0; j < i; j++)
recursion2(j/2);
}

public static void recursion2(int i) {


if (i > 1)
return;
else
recursion1(i);
}

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]

Section B – Applications (88 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. Some partial marks will be awarded
for correct answers not meeting the time complexity required.

- 3 of 13 -
CS2040 Midterm (AY2019/20 Semester 1)

5. Text manipulation [30 marks]

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]

E.g: Input string 𝑨𝑨: “This is CSS\b204000\b\b midterm\n”


Output text: “This is CS2040 midterm\n”

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]

E.g: Input string 𝑨𝑨: “This is CS2040 \Hmid\Eterm\n”


Output text: “midThis is CS2040 term\n”

- 4 of 13 -
CS2040 Midterm (AY2019/20 Semester 1)

- 5 of 13 -
CS2040 Midterm (AY2019/20 Semester 1)

6. Poison manufacturing facility [30 marks]

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 is a difficult sub-question]


Suddenly the facility suffers a rupture along the entire length of the pipes carrying the
poisonous liquid. The facility is built around a safety moat filled with chemicals that would
neutralize the poisonous liquid should it escape and flow into the moat, thus containing the
liquid from spreading to the outside world. However, the facility itself is not so lucky.

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.

Example top down view of the facility:

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)

Example side view of the facility (ignoring the side wall):

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).

In another example below

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)

7. Laser Defense [28 marks]

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)

b) [*This may be a difficult sub-question]


Company W has changed their laser defense so that the laser defenses are not activated
from the start and also the non-conflict property is no longer needed and now the number
of laser defenses 𝐾𝐾 is such that 4 <= 𝐾𝐾 <= 𝑚𝑚𝑚𝑚𝑚𝑚(𝑁𝑁, 𝑀𝑀). Sensors are placed on the
floor which will detect movement, so that whenever any intruder has moved a certain
distance within a floor the laser defenses will be activated. In this version, if 4 activated
laser defenses are placed in a rectangular configuration and an intruder is within that
rectangular region, the 4 laser defenses will erect a rectangular laser barrier to trap the
intruder. An intruder may be within the rectangular region of many such laser
configurations, but only the 4 laser defenses which forms the smallest rectangular region
trapping the intruder will be activated (if there are many such smallest configurations,
one of them will randomly be activated).

Given 4 laser defenses in some rectangular configuration, an intruder at (X1,Y1) is


considered within their rectangular region if

X coordinate of a leftmost laser < X1 < X coordinate of a rightmost laser


Y coordinate of a topmost laser < Y1 < Y coordinate of a bottommost laser

- 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.

(1,0) (1,1) (1,3) (1,4)


(1,0) (1,2) (1,3)
# #
(3,0) (3,2) (3,3) (4,1) (4,3)
(5,0) (5,4)

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 -

You might also like