Mandatory Office Hours Q56
Q56 MCQ from 2020 Collage Board
Q56
An online game collects data about each player’s performance in the game. A program is used to analyze the data to make predictions about how players will perform in a new version of the game.
The procedure GetPrediction (idNum) returns a predicted score for the player with ID number idNum. Assume that all predicted scores are positive. The GetPrediction procedure takes approximately 1 minute to return a result. All other operations happen nearly instantaneously.
Two versions of the program are shown below.
Version I
topScore
0
idList
[1298702, 1356846, 8848491, 8675309]
FOR EACH id IN idList
{
score
GetPrediction (id)
IF (score > topScore)
{
topScore
score
}
}
DISPLAY (topScore)
Version II
idList
[1298702, 1356846, 8848491, 8675309]
topID
idList[1]
FOR EACH id IN idList
{
IF (GetPrediction (id) > GetPrediction (topID))
{
topID
id
}
}
DISPLAY (GetPrediction (topID))
Version I calls the GetPrediction procedure once for each element of idList, or four times total. Since each call requires 1 minute of execution time, version I requires approximately 4 minutes to execute. Version II calls the GetPrediction procedure twice for each element of idList, and then again in the final display statement. This results in the procedure being called nine times, requiring approximately 9 minutes of execution time.
Both versions aim to achieve the same result, which is to find and display the highest predicted score among the players in idList. However, Version I directly updates the highest score (topScore), while Version II updates the ID of the player with the highest score (topID) and then retrieves the predicted score for that player. Version II essentially avoids calling GetPrediction multiple times for the same ID.
The answer: D - Version II requires approximately 5 more minutes to execute than version I