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

Lovely Professional University, Punjab

Academic Task No. 2

Name of the Faculty Member: Harsh Sharma || Teaching Assistant: Vasu Garg
Course Code: CSE230 || Course Title: Design and Analysis of Algorithms
Program: B-Tech (CSE) || Term: III || Section: K20MP
Max. Marks: 30 || Is Rubric Applicable: NA
Date of Allotment: 02/11/2021 || Date of Submission: 12/11/2021

Important Guidelines:
1. All questions in this Academic Task are compulsory.
2. It is mandatory to attempt all questions of the assignment in your own handwriting on A4 size sheets/pages
with a blue colour ink pen. Any other mode of attempt (typed or printed codes or table) except
handwritten/drawn will not be accepted/considered as valid submission(s) under any circumstances.
3. Every attempted sheet/page should carry clear details of student such as Name, Registration number, Roll
number, Question number and Page number. The page numbers should be written clearly on the bottom of
every attempted sheet in a prescribed format as: for page 1; Page 1 of 4, for page 2; Page 2 of 4, for page 3;
Page 3 of 4 and for page 4; Page 4 of 4, in case your assignment/document is of 4 pages.
4. After attempting the answer(s), student needs to take photograph of each of these answer sheets/pages and
needs to convert the jpeg format images into a single pdf format document (can be done with many free
online available converters).
5. This PDF file should be uploaded onto the UMS interface on or before the last date of the
submission. 6. Refrain from indulging into plagiarism as copy cases will be marked zero.

6. Refrain from indulging into plagiarism as copy cases will be marked zero.

7. This Document contains 2 sets of Paper, namely Set A and Set B

8. Set A is for learners having Odd Roll Number and Set B is for learners having Even Roll
Number. Learners must solve and upload papers accordingly. Solving a wrong set would be
consider a failed attempt to solve CA1 and will be given 0 marks.
SET A

Short answer type question: 16 marks (4*4)

1. Suppose you have to sort a list that consists of a sorted list followed by some random elements. Which would be
most suitable for such a task out of bubble sort, insertion sort, quick sort and selection sort? Explain that sorting
algorithm technique.
**************************************************************************************
2. Suppose you have to sort a given data and you have to choose an efficient sorting algorithm. Compare the different
sorting algorithms that you have studied in terms of their time complexity in different cases.
**************************************************************************************
3. What is the difference between a stable and an unstable sorting algorithm. Explain any one stable sorting
algorithm in detail.
**************************************************************************************
4. Assume you are giving an interview and the interviewer asks you to explain quick sort algorithm with the
help of an example. Explain your answer in detail.
**************************************************************************************

Long answer Type questions: 14 marks (2*7)

1. Suppose you are participating in a coding event, and you have been given the following problem to solve. You are
given n non-negative integers representing an elevation map where the width of each bar is 1, you have to compute
how much water it can trap after raining.

Example 1:

Input: height = [0,1,0,2,1,0,1,3,2,1,2,1]


Output: 6
Explanation: The above elevation map (black section) is represented by array [0,1,0,2,1,0,1,3,2,1,2,1]. In this case, 6
units of rain water (blue section) are being trapped.

Example 2:
Input: height = [4,2,0,3,2,5]
Output: 9
**********************************************************************************************

2. Suppose you are given an array of distinct integer candidates and a target integer target, you have to return a list of
all unique combinations of candidates where the chosen numbers sum to target. You may return the combinations in
any order. The same number may be chosen from candidates an unlimited number of times. Two combinations are
unique if the frequency of at least one of the chosen numbers is different.
It is guaranteed that the number of unique combinations that sum up to target is less than 150 combinations for the
given input.

Example 1:

Input: candidates = [2,3,6,7], target = 7

Output: [[2,2,3],[7]]

Explanation:
2 and 3 are candidates, and 2 + 2 + 3 = 7. Note that 2 can be used multiple times.
7 is a candidate, and 7 = 7.
These are the only two combinations.

Example 2:

Input: candidates = [2,3,5], target = 8

Output: [[2,2,2,2],[2,3,3],[3,5]]
SET B

Short answer type question? 16 marks (4*4)

1. Suppose you have to sort a given data and you want to increase the efficiency of your code. Your friend tells you to
use an in place sorting algorithm. Explain what is in place sorting and why it should be used.
**********************************************************************************************
2. Suppose you are given insertion sort, bubble sort, merge sort and quick sort and you have to choose a sorting
algorithm which is not a stable sorting algorithm in its typical implementation. Which one would you choose?
Explain.
**************************************************************************************
3. Assume you are giving an interview and the interviewer asks you to explain merge sort algorithm with
the help of an example. Explain your answer in detail.
**************************************************************************************
4. Suppose you have to apply a sorting algorithm on an array given to you. You are confused between
bubble sort and selection sort. Differentiate between both the algorithms to find out which one to use.
**************************************************************************************

Long answer Type question. 14 marks (2*7)

1. Suppose you are participating in a coding event and you have been given the following problem to solve. You are
given an array of integer heights representing the histogram's bar height where the width of each bar is 1, you have to
return the area of the largest rectangle in the histogram.

Example 1:

Input: heights = [2,1,5,6,2,3]


Output: 10
Explanation: The above is a histogram where width of each bar is 1.
The largest rectangle is shown in the red area, which has an area = 10 units.

Input: heights = [2,4]


Output: 4
**********************************************************************************************

2. Suppose you are working in a trading company and you have to develop a program to maximise profits from
stocks. You are given an array, prices where prices[i] is the price of a given stock on the ith day.
You want to maximize your profit by choosing a single day to buy one stock and choosing a different day in the future
to sell that stock.
Return the maximum profit you can achieve from this transaction. If you cannot achieve any profit, return 0.

Example 1:

Input: prices = [7,1,5,3,6,4]

Output: 5

Explanation: Buy on day 2 (price = 1) and sell on day 5 (price = 6), profit = 6-1 = 5.
Note that buying on day 2 and selling on day 1 is not allowed because you must buy before you sell.

Example 2:

Input: prices = [7,6,4,3,1]

Output: 0

Explanation: In this case, no transactions are done and the max profit = 0

You might also like