0% found this document useful (0 votes)
11 views2 pages

CS300 2020 Fall Final Exam 1 PDF

Uploaded by

slymn13
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)
11 views2 pages

CS300 2020 Fall Final Exam 1 PDF

Uploaded by

slymn13
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/ 2

CS300 2020 FALL FINAL EXAM

Q4 - Give an algorithm (i.e., step by step descriptions in English and/or in


pseudocode) with an average complexity of O(N) to find the median number in
an array of floating point numbers (not necessarily sorted).

For this problem, we can sort the array and return the element in the middle,
but it would have O(nlogn) time complexity. Thus we can try following an
approach similar to quicksort. We can choose a random pivot and use the
same partitioning algorithm but we only recur for the side that the median
element is in and stop when we found the median element. This would result
in O(n) average time complexity.

Q5 - Give an algorithm (i.e., step by step descriptions in English and/or


pseudocode) with an average complexity of O(log N) to search for a number in
a sorted (in increasing order) array of N numbers.

l = 0, r = n - 1
while l <= r
mid = l + (r - 1) / 2
if arr[mid] == searchedNumber
return mid
if arr[mid] < x
l = mid + 1
else r = mid - 1

return -1

Where arr contains the sorted array and searchedNumber is the number we
are searching for. Assuming indexes start from 0.

Q6 - Give an O(N) time algorithm (i.e., step by step descriptions in English


and/or pseudocode) that uses O(1) amount of additional memory, to remove
duplicate items from a given linked list of items (not necessarily sorted).
(SORU IPTAL EDILDI)

We could use two while loops where we choose the node in one loop, and
find and remove the nodes that are the same in the other loop, but this would
have O(n^2) time complexity.

This study source was downloaded by 100000770618860 from CourseHero.com on 01-08-2023 14:00:29 GMT -06:00
1

https://www.coursehero.com/file/147828732/CS300-2020-FALL-FINAL-EXAM-1pdf/
CS300 2020 FALL FINAL EXAM

We can also sort the linked list, then go through the nodes and remove a node
if it’s the same as the previous node. This would have O(nlogn) time
complexity.

We can also use a hashset to store the seen nodes. We would iterate through
the nodes and if the current node is in the seen hashset, then we would
remove it. Otherwise, we would add it to the hashset. This would result in O(n)
time complexity but we would be using O(n) additional memory since we are
using a hashset.

Q7 - You are given with a collection of N activities, each of which is specified


with their start and finish time. Given that a single person can only work on a
single activity at a time:

a) Give a greedy algorithm (i.e., step by step descriptions in English and/or


pseudocode) to determine the maximum number of activities that can be
performed by a person.

Sort the collection of N activities based on their finishing time in


ascending order.
Append the first activity from the sorted array to the solution array.
For i = 1 to N - 1 {
If the start time of the ith activity in the sorted array is greater than or
equal to the finishing time of the last activity in the solution array,
append the ith activity to the solution array.
}
Return the length of the solution array

b) What is the complexity of your algorithm?

The sorting has O(NlogN) time complexity, while the remaining of the
algorithm has O(n) time complexity since we have a for loop that loops N
- 1 times. So the time complexity is O(NlogN + N) but N can be ignored -
> O(NlogN)

This study source was downloaded by 100000770618860 from CourseHero.com on 01-08-2023 14:00:29 GMT -06:00
2

https://www.coursehero.com/file/147828732/CS300-2020-FALL-FINAL-EXAM-1pdf/
Powered by TCPDF (www.tcpdf.org)

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy