Questions And Answers With Verified Updates
Attempt #1 Status: Passed
1. Consider the following pseudocode that merges two lists of numbers into one:
Merge0(List1, List2)
Set OUT list to empty
While List1 is not empty OR List2 is not empty
If one list is empty and the other is not,
Remove the first number from the non-empty list and add it to OUT list
If both lists are non-empty,
Remove the first number from List1 and add it to OUT list
Remove the first number from List2 and add it to OUT list Return OUT list
If List A is [1, 3, 5] and List B is [2, 4, 6] then what is the result of Merge0 (List A, Merge0 (List B, List A))?
YOUR ANSWER CORRECT
ANSWER
[ 1, 2, 3, 4, 5, 6 ]
[ 1, 1, 3, 2, 5, 3, 4, 5, 6 ]
[ 2, 1, 4, 3, 6, 5 ]
[ 1, 2, 3, 1, 5, 4, 3, 6, 5 ]
2. Given this pseudocode:
S = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20} x =
2 While(x<11):
For i in S:
If 0 ≡ i mod x and i ≠ x:
delete i from S
end-If
end-For
x=x+1
end-While
What is S at the end of this code?
,2. YOUR ANSWER CORRECT
ANSWER
∅
3.
{2, 3, 5, 7}
{11, 13, 17, 19}
{2, 3, 5, 7, 11, 13, 17, 19}
Given the pseudocode fragment:
x := 2 count := 4
while (count > 0)
x := 2 × x
count := count - 1 End-while
What is the final value for x?
CORRECT
YOUR ANSWER ANSWER
4
16
32
64
Given this pseudocode that extracts a sample sequence from the data sequence of length N:
Function Sampler (Sequence Data)
Set Sample to an empty sequence
Set N to the length of Data
While N>=1
Append element N of Data to Sample
N:= N/2
Return Sample
What is the worst-case run time for Function Sampler?
YOUR CORRECT
ANSWER ANSWER
, ( )
O(N/2)
O(N)
O(Nlog2N)
, 4.
Given this algorithm:
Simple Sort
This algorithm sorts the elements of an array.
Input: numb, an array of n integers Output:
numb, in ascending order
for i = 1 to n for
j = 1 to n – i
if numb(j) > numb(j + 1)
temp = numb(j)
numb(j) = numb(j + 1)
numb(j + 1) = temp
end for
end for
What is the asymptotic worst-case complexity?
YOUR CORRECT
ANSWER ANSWER
O(1)
O(n)
( )
O(n log(n))
Given the pseudocode:
procedure bubbleSort( A : list of sortable items )
n = length(A)