You are on page 1of 4

UE101: Practice Final Exam

Date: TBD Time: TBD Max. Marks: 50


Name:

Full S. R. No:

Instructions:

1. Students are required to sit in their assigned seats only.


2. Mobile phones/any form of communication devices are strictly prohibited in the exam rooms. It
is best to leave the mobile phone in the hostel itself before coming for the exam.
3. Mere possession of a mobile phone/communication device inside the examination hall or during
the exam will be treated as a case of cheating (absolutely no excuses). In such cases, suitable
action will be taken.
4. With the exception of one A4-sized cheat sheet (double-sided, handwritten or typed in any font
size), no form of unfair means (including talking to another student, copying from another
student’s paper, copying from any books, use of mobile phone/communication devices) during the
examination will be tolerated. A student found resorting to any form of unfair means during the
examination, will be given ‘F’ grade in that course as a minimum punishment. No appeals will
be accepted.
5. Abetting a student to resort to any form of unfair means will also be considered as an unfair
practice. In this instance as well, the student abetting another student will be given ‘F’ grade in
that course. No appeals will be accepted. In case the student who is abetting another student is
from a different class/batch, suitable action will be taken on such a student.
6. Before answering the questions, the student must write his/her name and student registration
number on the top of this page.
7. When an additional sheet is taken by the student, the student must write his/her name and serial
number, sign the additional sheet and must get it countersigned by the invigilator.
8. Only non-programmable calculators will be allowed during the examinations; programmable
calculators are strictly prohibited.
9. Exchange of calculators between one and the other is not permitted.
10. If a student is found with a mobile phone/communication device while taking a break to use the
washroom when the exam is in progress, it will be treated as a case of cheating. Irrespective of
whether the student is using the mobile phone/communication device or not, the same penalty as
in item 4 will be applicable.
Question 1 (15 marks): Your friend has written the following function to determine the
maximum profit that can be earned by buying one unit of some asset on a day 𝑖 and selling it
on a later day 𝑗 > 𝑖, given the daily price of the asset as a non-NULL array of n integers.
1 int max_profit(int n, int price[n]) {
2 int ans = 0;
3 for (int i = 0; i < n; i++) {
4 for (int j = 0; j < n; j++) {
5 if (price[j] - price[i] > ans) {
6 ans = price[j] - price[i]; // more profit!
7 }
8 }
9 }
10 return ans;
11 }
There are no compiler errors/warnings when the above code is compiled with the -Wall flag.
Even so, the function is incorrect.
a) Suppose max_profit() is called from main() as follows:
int price[3] = {500, 300, 400};
int ans = max_profit(3, price);
Show that ans will be calculated incorrectly.

b) Your friend replaces j < n with j > i && j < n on Line 4. Clearly explain whether
this change makes the function better (e.g., it fixes the error, or it improves the function in
some other way) or worse (e.g., it creates a new error/warning).

c) What is the worst-case time complexity of the original and modified max_profit()
function in terms of the array length n? Justify your answers.

d) Rewrite the max_profit() function correctly, making as few changes as possible. For
each change, write a short comment explaining your reason for the change.

e) Recalculate the worst-case time complexity of your modified max_profit() function.


Question 2 (5 marks): Your friend suggests a faster algorithm for solving the above problem
using a modified array implementation for the priority queue Q with one extra operation:
min(Q). This function returns but does not extract the smallest (i.e., highest-priority) number
in Q (i.e., the number at index 0 in the array).
i. Initialize Q to be a priority queue containing only price[0]
ii. Initialize ans = 0
iii. For every index i from 1 to n-1:
enqueue(Q, price[i])
ans = max (ans, price[i] - min(Q))
iv. Return ans
Exactly one of these statements is true. Identify the true statement and clearly explain why it
is true.
• Statement 1: The above algorithm is asymptotically faster than the modified version of
max_profit() in Question 1.
• Statement 2: The above algorithm is incorrect.
Question 3 (10 marks): The above algorithm only requires the enqueue() and min()
operations. Describe a “better” data structure than a priority queue that supports only these two
operations. (Note: Here, “better” can mean simpler, more time efficient, more space efficient,
etc. Clearly explain the sense(s) in which your data structure is better.)
Question 4 (10 marks): The version of Dijkstra’s algorithm presented in class does not work
correctly for weighted graphs with negative-weight edges, even when there are no negative-
weight cycles.

To verify this, run Dijkstra’s algorithm on the above graph, with vertex a as the source and
identify exactly where and why the algorithm fails. For every vertex, process its neighbours in
alphabetic order. For example, process vertex c’s neighbours in the order: a, b, d, e.
Question 5 (10 marks): Consider the following variation of the job scheduling problem where
there are 𝑛 jobs. We can only work on one job at a time, and we can start a new job immediately
after finishing the previous job. Each job 𝑗 takes time 𝑡𝑗 > 0 and has a value 𝑣𝑗 > 0. We can
start job 𝑗 at any time 𝑡, and we will finish it at time 𝑡 + 𝑡𝑗 (since it takes time 𝑡𝑗 to complete).

Given the information (𝑡𝑗 , 𝑣𝑗 ) for each of the 𝑛 jobs, we want an efficient algorithm to find the
maximum value that can be earned doing jobs whose total time is ≤ 𝑇.
Show that the following “most-value-per-unit-time job first” greedy strategy does not work:
v. Initialize 𝑡 = 0 and value = 0
vi. Sort jobs in descending order of value-per-unit-time 𝑣𝑗 /𝑡𝑗
vii. For each job 𝑗 in this order:
If 𝑡 + 𝑡𝑗 ≤ 𝑇 then:
Update 𝑡 = 𝑡 + 𝑡𝑗 // do job 𝑗
Update value = value + 𝑣𝑗
viii. Return value
Your answer must be a counter-example which provides the following information:
• The number of jobs 𝑛 (a positive integer)
• For each job 𝑗, the time to do the job 𝑡𝑗 and its value 𝑣𝑗 (both must be positive real numbers)
• The correct answer (along with an explanation)
• The incorrect answer returned by the above algorithm (trace the execution of the algorithm
on your counter-example)

You might also like