Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

CS 300: ADVANCED PROGRAMMING

SPRING 2024

Module: TypeScript

Exam

March 17, 2024 (Sunday)

1
Exam Guidelines
- The Exam is due at 3 PM.
- We will enable LMS access to you at 3 PM, and you will have 15 minutes (until 3:15PM) to submit
your exam. If you finish your exam early, you will be allowed to leave, but you must return to submit
your exam. However, once you leave the lab, you can only return after 3:05 PM, so you will have 10
minutes to submit your files. Again, it is your responsibility to ensure the safety of your files. If your
system reboots or something unexpected happens, the course staff cannot help you.
- There are a total of 7 questions.

- Total marks are 80.


- Folder src contains all the functions that you have to implement. And folder test contains the test
cases.
- To run the tests, run the following command in the root directory:
npm test

- To run a specific test, run the following command in the root directory:
npm test <testname>
- If you want to have ‘.ts‘ files compile to ‘.js‘ files automatically, for manual testing, run the following
commands:
npm run build
node build/<filename>.js
- Run the below command to remove all the compiled files:
git run clean
- Please note that the test cases may not cover all the cases for the questions. There may be other
(corner) cases which students have to figure out themselves.
- Plagiarism is strictly prohibited. Students are not allowed to discuss their solutions with others
or look over the internet. However, they may seek help from the course staff ONLY.
- There will be NO partial marking for any of the questions. This is being done to be fair to all the
students in this course. However, the questions will be graded according to the number of test cases
that are passing. For example: if a 10 marks question has 10 test cases and 5 of them are passing for
a student, then they will get 5/10 for that question.
- Happy Coding!

2
Question 1: Mountain Climber (5 Marks)

A climber of great stamina, skill, and daring has decided to climb as many
mountains as possible. However, this otherwise unbeatable climber has an
Achilles’ heel: Once he has climbed a mountain, he can only climb
a mountain of greater height! What he has in brawn, he lacks in brain,
so he has hired you to help him figure out the number of mountains he can
climb in a given range. Given an array of mountain heights called mountains,
return the maximum number of mountains our intrepid climber can climb.
Sample Input: Heights of Mountains = [4, 3, 2, 3, 1, 4, 5, 6, 11, 23]
Sample Output: 6
Explanation: The mountains that the climber climbs are [1,4,5,6,11,23]
Sample Input: Heights of Mountains = [1, 2, 1, 2, 2, 1, 3]
Sample Output: 2
Explanation: The mountains that the climber climbs are [1,2] or [1,3]. Both
have equal length.
Sample Input: Heights of Mountains = [5, 5, 5, 5, 5, 5]
Sample Output: 1
Explanation: The climber climbs only one mountain of height 5.

Question 2: Last Stone Weight (5 Marks)

You are given an array of integers stones where stones[i] is the weight of
the ith stone.
We are playing a game with the stones. On each turn, we choose the heaviest
two stones and smash them together. Suppose the heaviest two stones have
weights x and y with x ≤ y. The result of this smash is:
• If x == y, both stones are destroyed.
• If x != y, the stone of weight x is destroyed, and the stone of weight y
has new weight y - x.
At the end of the game, there is at most one stone left.
Return the weight of the last remaining stone. If there are no stones left,
return 0.
Sample Input: stones = [2,7,4,1,8,1]
Sample Output: 1

3
Explanation: We combine 7 and 8 to get 1 so the array converts to [2,4,1,1,1]
then,
we combine 2 and 4 to get 2 so the array converts to [2,1,1,1] then,
we combine 2 and 1 to get 1 so the array converts to [1,1,1] then,
we combine 1 and 1 to get 0 so the array converts to [1] then that’s the value
of the last stone.

Question 3: Maximizing Profit (10 Marks)

You are given an array, prices, where prices[i] is the price of a given stock
on the ith day.Find the maximum profit you can achieve. You may complete
at most two transactions.
Note: You may not engage in multiple transactions simultaneously (i.e., you
must sell the stock before you buy again).

Sample Input: prices = [3,3,5,0,0,3,1,4]


Output: 6
Explanation: Buy on day 4 (price = 0) and sell on day 6 (price = 3), profit
= 3-0 = 3. Then buy on day 7 (price = 1) and sell on day 8 (price = 4),
profit = 4-1 = 3.
Sample Input: prices = [1,2,3,4,5]
Output: 4
Explanation: Buy on day 1 (price = 1) and sell on day 5 (price = 5), profit
= 5-1 = 4. Note that you cannot buy on day 1, buy on day 2 and sell them
later, as you are engaging multiple transactions at the same time. You must
sell before buying again.
Sample Input: prices = [7,6,4,3,1]
Output: 0
Explanation: In this case, no transaction is done, i.e. max profit = 0.

Question 4: A Concurrency Conundrum (10 Marks)

Congratulations! You just got your first job. Now your manager has asked
you to perform a bunch of tasks on your first day, but HR has a strict rule
that no employee can perform more than n tasks at the same time. You are
given an array of tasks; each task is an object with a unique taskID and the

4
duration (in milliseconds) it will take to complete the task. Your job is to
perform these tasks in the order they are given to you. Once you’ve com-
pleted all tasks, return an array containing the taskIDs in the order they
were completed. You’ll need to handle the concurrency control and ensure
that no more than n tasks are running simultaneously.

Sample Input: tasks = [ {taskID: 1, duration: 1000}, {taskID: 2, duration:


2000} , {taskID: 3, duration: 500} ] , n = 2
Output: [1,3,2]
Explanation: Tasks 1 and 2 will be performed concurrently at the begin-
ning since n = 2. Once task 1 is completed after 1000 milliseconds, task 3
will be started. Then 500 milliseconds later, task 3 will finish, and 500 more
milliseconds later, task 2 will finally be completed. Thus the order in which
the tasks are completed is 1 followed by 3 followed by 2, giving us the output
[1, 3, 2].

Question 5: Tree Construction Revisited (15 Marks)

Given a pre-order traversal and an in-order traversal of a binary tree, con-


struct the tree.
You can assume that the values in the tree are distinct.
Sample Input: pre-order = [1, 2, 5, 3, 6], in-order = [2, 5, 1, 6, 3]
Output:

5
Question 6: Optimizing Packet Delivery Probability (15 Marks)

In a network, when transferring a packet from one router to another, each


link between routers has a specific probability of successfully delivering the
packet. Given these probabilities, there could be multiple routes to deliver
a packet from the source router to the destination router. Your objective is
to identify the route that maximizes the overall probability of the packet’s
successful delivery. This involves calculating the maximum product of prob-
abilities for all links in the chosen path. You have to return this maximum
delivery probability.
Example: Imagine we have 4 routers in our network, labeled as 0, 1, 2, and 3.
The connections between these routers along with their delivery probabilities
are as follows:
• Router 0 connects to:
– Router 1 with a probability of 0.9
– Router 2 with a probability of 0.7
• Router 1 connects to:
– Router 3 with a probability of 0.8
• Router 2 connects to:
– Router 1 with a probability of 0.6
– Router 3 with a probability of 0.9
• Router 3 has no outgoing connections.
The goal is to find the path from a source router (e.g., 0) to a destination
router (e.g., 3) that maximizes the probability of packet delivery.
You are given the network information in a file. Here is how this network is
represented in the file.

6
Each line in the file starts with a router’s ID, followed by a list of con-
nections to other routers and the likelihood of each connection working. For
instance, router 0 can connect to router 1 with probability 0.9 and to router
2 with probability 0.7. If a router’s list is empty, like for router 3, it means
that router doesn’t directly connect to any other routers.

In this scenario, there are two possible paths ( 0 → 1 → 3 and 0 → 2 → 3).


Probability of the first path is 0.9*0.8 = 0.72 and probability of the second
path is 0.7*0.9 = 0.63. So, we return 0.72

Note: you have to round the result to two digits after the decimal point
using the toFixed() method, which is part of the ‘Number‘ prototype. For
example,
let a : number = 0.719928;
a.toFixed(2) will give 0.72

You are required to read the file and return a Promise. Function signature
to perform this is already given and you are required to use it. You can
import ’fs’ to read the file.

Question 7: Scales of Injustice (20 Marks)

Imagine a grand scale and a set of weights. You could put any number
of weights on either side of the scale to create a differential that helps you
measure a given weight. With the following weights 1, 3, and 9, you could
weigh all the weights up to 13 kg.
Now, if you always want to use the left side to weigh the actual object. This
is how it would play out.

7
Finding the next weight in this sequence [1,3,9] is trivial and intuitive. You
are given a list containing three positive integers [1, a, a2 ] (just like [1,3,9])
and a positive integer n where 3 <= n <= 12. Your task is to determine the
first n weights in the sequence, starting with [1, a, a2 ], and use these weights
to complete the above table. Once that is done, find the sum of the right
side and subtract it from the sum of the left side. The result is your answer.
In the above example, the left side is 13 and the right side is 104. The dif-
ference is 91 which is the correct answer.

You might also like