ExamII (Fall2019)
ExamII (Fall2019)
Welcome to Exam II
Thursday December 12, 2019
Name: _______________________
Instructions:
Good Luck!!
2
2. An algorithm of order O(log2n) is considered asymptotically faster than an algorithm of order O(√𝑛)
Answer: True False
3. 𝑁 is 𝛺(𝑁 )
Answer: True False
5. 2𝑁 is 𝛳(𝑁)
Answer: True False
6. A total of 10 recursive method calls are needed to recursively calculate the Fibonacci of 4.
Answer: True False
7. 𝑁 is 𝑂(2 )
Answer: True False
9. Consider an algorithm that matches parentheses using a stack. The maximum number of parentheses
that the stack will hold at any given moment when the algorithm analyzes the following sequence
is 3: (( )(()))
Answer: True False
10. In a circular drifting array-based implementation of a queue data structure, the dequeue operation
requires O(n) time, where n is the size of the queue.
Answer: True False
11. Suppose we have a circular drifting array-based implementation of a queue with 11 items stored in
the queue at indexes 2 through 12. Assume also that the array used to realize the queue has a
capacity of 13. In light of these facts, an enqueue operation would place a new element at index 0
of the queue.
Answer: True False
13. Consider the case where a method called f1 that calls a different method called f2, which then
calls the original method f1. In this case, f1 is considered as a recursive method.
Answer: True False
14. The peek method of the java.util.Stack class returns the top element of the Stack.
Answer: True False
}
(2) Write a recursive function that returns a String made up of the elements stored in an
ArrayList called al listed in reverse order. For instance, if the list contains the values
[1, 2], then the output String should be “2 1”. The function takes as a parameter a
reference to the ArrayList. Check the appendix for more details about the methods
offered by the ArrayList class.
String reverseToString(ArrayList<Integer> al) {
if(al.size() == 0)
return “”;
else if(al.size() == 1) {
return al.remove(0) + "";
} else {
Integer val = al.remove(0);
String str= reverseToString(al);
return str + " " + val;
}
}
4
2. Unfold the following recurrence relation and obtain a big-Oh characterization, in terms of
N, for T(N). Show your work.
T(N) = 2T(N/2) + N
T(1) = 1
𝑁 𝑁 𝑁 𝑁
𝑇(𝑁) = 2 × 𝑇 +𝑁 =2× 2×𝑇 + +𝑁 =2 𝑇 +𝑁+𝑁
2 4 2 4
𝑁 𝑁 𝑁 𝑁
=2 2×𝑇 + + 2𝑁 = 2 𝑇 + 3𝑁 = ⋯ = 2 𝑇 + 𝑘𝑁
8 4 8 2
return output;
}
2. Implement a method that destructively copies each value in an ArrayList, resulting in
an ArrayList that is twice as long. For example, if the input list contained [1, 2, 3], the
resulting list should contain [1, 1, 2, 2, 3, 3] after the call to this method.
ArrayList<Integer> copyDestructively(ArrayList<Integer> list){
ArrayList<Integer> output = new ArrayList<Integer>();
return output
}
6
System.out.println("Enter k: ");
k = scan.nextInt();
while(queue.size() > 1) {
System.out.println("Queue: " + queue);
for(int i=1; i<=k; i++)
queue.enqueue(queue.dequeue());
System.out.println(queue.dequeue() + " is out");
}
winner = (String) queue.dequeue();
} catch (QueueException e) {
winner = "Could not solve the problem";
}
return winner;
}
}
10