A statistician developed this procedure to calculate the "variance" of a list of numbers. The variance is a
statistical quantity that corresponds to the average of the sum of the squared differences of each
number from the mean.
As input, the procedure takes a list of numbers and its mean:
PROCEDURE calculateVariance(numbers, mean) {
count ← 0
sumSquaredDiffs ← 0
FOR EACH num IN numbers {
diff ← (num - mean)
squaredDiff ← diff * diff
sumSquaredDiffs ← sumSquaredDiffs + squaredDiff
count ← count + 1
variance ← sumSquaredDiffs / count
}
RETURN variance
}
The statistician verifies the procedure outputs the variance correctly, but they still want to improve the
efficiency of the procedure by reducing the number of operations required.
Which change will reduce the most number of operations while still outputting a correct answer? -
AnswersMoving the calculation of variance to be after the loop (but before the return)
The following algorithm computes the maximum score for a list of final exam scores.
Initialize a variable max to the first score in the list.
For each score in the list, compare the score to max.
If score is greater than max, store score in max
Return max.
Which building blocks are involved in this algorithm?
, Note that there may be multiple answers to this question. - AnswersSelection
Iteration
Sequencing
ScootALot is a scooter rental service. At the end of each day, they hire contractors to pick up scooters
and distribute them optimally around the city.
The distribution algorithm considers all the possible locations for the scooters, compares that to the
density of customers, and comes up with the optimal location for each scooter.
As the company becomes more popular, they realize their algorithm is taking an unreasonable amount
of time to come up with optimal scooter locations.
What is the best way to improve the run time of the algorithm? - AnswersUse a heuristic-based
algorithm that suggests good locations for the scooters.
An algorithm will be used to calculate the difference between the smallest and largest values in a list.
For the list of [10, 3, 5, 6], it should calculate a difference of 7.
There are two proposals for the algorithm:
Algorithm 1: Set minVal to the first value in the list and maxVal to the last value in the list. Iterate
through each number in the list. If the number is greater than maxVal, store it in maxVal. If the number
is less than minVal, store it in minVal. After loop, set maxDiff to the difference between maxVal and
minVal.
Algorithm 2: Set minVal to 1000 and maxVal to 0. Iterate through each number in the list. If the number
is greater than maxVal, store it in maxVal. If the number is less than minVal, store it in minVal. After loop,
set maxDiff to the difference between maxVal and minVal.
Which of these statements are true about these algorithms?
I. Algorithm 1 does not work on lists where the smallest valu - AnswersII only
The flow chart below visualizes an algorithm to generate the Fibonacci numbers, a famous mathematical
sequence.
If the variable max is set to 5, what would be displayed as a result of executing this algorithm? -
Answers1 2 3 5 8
Yuto implements an algorithm for finding the "greatest common divisor" of two numbers. For the
numbers 54 and 24, it finds a greatest common divisor of 6.
He counts the number of steps required by the algorithm for numbers of increasing digit length and
comes up with this table:
, Number of digits Steps
24
36
48
5 10
6 12
Based on the table, which of the following statements describe the run time for this algorithm?
Note that there are 2 answers to this question. - AnswersThe algorithm runs in polynomial time.
The algorithm runs in reasonable time.
An online app for playing Chess utilizes a variety of algorithms. Their software engineers would like the
algorithms to run in under a second so that users have a great experience.
Which algorithm's runtime is most likely to be improved by the use of a heuristic? - AnswersChoosing a
good move when the computer is playing against the human
A restaurant delivery app uses the following algorithm to choose the restaurants it features to each user.
Which pseudocode is equivalent to the algorithm in the flowchart? - AnswersIF (openNow = true AND
milesAway < 5 AND reviews > 3.5) {
showFeatured ← true
} ELSE {
showFeatured ← false
}
The algorithm below determines whether a given year is a leap year.
If year is not divisible by 4, set leap_year to false
Else if year is not divisible by 100, set leap_year to true
Else if year is not divisible by 400, set leap_year to false
Else, set leap_year to true.