Professional Documents
Culture Documents
Haskell Exam
Haskell Exam
SPRING 2024
EXAM 1
Module: Haskell
1
Exam Guidelines
- The exam is due at 4:15 PM.
- We will enable LMS access to you at 4 PM, and you will have 15 minutes (until 4: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 4: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 10 questions.
- We have provided you with Code.hs. You will write your code in it.
- Total marks of the exam are 85.
- 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.
However, they may seek help from the course staff ONLY.
- Handwritten notes are allowed.
- We have provided you with the Prelude documentation so you can use built-in functions as you like.
- 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.
- You must submit the Code.hs only using the naming format: roll number.hs. For example, if your
roll number is 23100201 then your file name should be ‘23100201.hs’
- Good Luck!
2
Question 1: Kth Smallest Element in a BST (5 Marks)
Given a binary search tree, and an integer k, return the Kth smallest value
of all the values of the nodes in the tree. Assume that the input is always
valid.
K =2 , Output: 2
K = 4, Output: 4
3
Output: [4,5,6,7,8,1,2,3]
4
it.
Sample Input: string = “19”
Output: 2
Explanation: One way to decode this is ”AI” i.e, (1 9). Other is ”S” i.e,
(19).
Sample Input: string = “2021”
Output: 2
Explanation: One way to decode this is ”TBA” i.e (20 2 1). Other is ”TU”
i.e, (20 21).
5
Similarly, the next permutation of arr = [2,3,1] is [3,1,2].
The next permutation of arr = [3, 2, 1] is [3, 2, 1].
6
Example 1:
7
Question 10: Blasts and Fire (17 Marks)
Imagine setting a tree on fire and letting the flames spread until the tree
completely vanishes. We explored this scenario and discovered it’s relatively
straightforward to predict when a tree will disappear after igniting a fire on
one of its nodes. What intrigued us more was considering a LinkedList of
Trees. Yes, you read that correctly—a LinkedList containing Trees. The
fire starts from the longest leaf in one of these trees, based on a given index
indicating from which tree the fire begins. You can assume that there is only
one longest leaf. The index starts at 0, with 0 referring to the first tree in
the LinkedList. You can assume the index will always be valid.
After each time unit, a node’s value decreases by one, and the fire spreads
to its neighboring nodes (either parent and/or children). A node and all its
descendants are destroyed when its value drops to 0. All node values are
non-negative.
Once the longest leaf and the root of a tree catch fire, the flames will spread
to the root of the adjacent tree(s) after one time unit. For example, if the
longest leaf and root of the tree at index 2 catch fire, after one time unit, the
fire will spread to the roots of the trees at indexes 1 and 3, assuming there’s
a tree at index 3. The fire will then spread to the root of the tree at index 0
8
after a one-unit delay once the longest leaf and the root of the tree at index
1 catch fire.
Here are some of the guidelines:
• When deciding between the spread of fire and the effects of an explosion,
prioritize the fire spread. For instance, if a tree yet to catch fire is exposed
to fire from one side and an explosion from another, the fire will take
precedence. As a result, the unaffected tree will catch fire and then
immediately suffer the explosion’s effects, halving all its values (both of
these events happen simultaneously, but we prioritize fire spread).
• Similarly, when choosing between reducing a node’s value due to fire and
the effects of an explosion, reduce the value first. For example, if a tree’s
root is on fire with a value of 8 and its neighbor explodes, first reduce the
value to 7, then apply the explosion effect, halving it to 3.5, and apply
floor rounding after the division, so the final value becomes 3..
• If explosions occur from both neighbors simultaneously, divide the values
by 4.
• If an explosion triggers a chain reaction of explosions across neighboring
trees, these should occur simultaneously. For instance, if the tree at index
0 causes an explosion, leading to another at index 1, and then to another
at index 2, all explosions should be considered to happen at the same
moment.
Your task is to determine the time at which the entire LinkedList has com-
pletely vanished.
Consider an example where a LinkedList contains four trees, and a fire starts
from the longest leaf (node with a value of 19) of the tree at index 2.
9
After 4 time units, the fire spreads to the neighboring trees at indexes 1 and
3. The list is updated accordingly.
Then, 3 time units later, the fire reaches the tree at index 0. This occurs 1
time unit after the longest leaf of the tree at index 1 catches fire, requiring 2
time units for the fire to travel from the root to the longest leaf. The list is
updated again to reflect this situation.
After an additional 2 time units, the value of the root node at index 1 drops
to zero, causing a blast.
Consequently, all the values of the nodes in the tree at index 0 and at index
2 are halved. Because we don’t have any tree at index 1, trees - in the above
10
tree - at indexes 0 and 2 will become neighbors. Therefore, 9 (4+3+2) time
units after the fire starts in the LinkedList, the state of the LinkedList is as
follows.
Furthermore, 1 time unit later, the value of the root node of the tree at index
1 falls to 0, resulting in another blast.
Again, trees - in the above tree - at indexes 0 and 2 will become neighbors.
Subsequently, 1 time unit after that, the values of the root nodes of both of
these trees become zero.
Thus, the entire LinkedList vanishes after a total of 11 time units.
11