Exam II - Fall21 (2nd Section) - Sol
Exam II - Fall21 (2nd Section) - Sol
Name: _______________________
Instructions:
Good Luck!!
2
9) Which of the following methods of the BigInteger class can be used to convert a primitive
value into a BigInteger?
a. valueOf
b. intValue
c. toString
d. value
10) If the characters ‘D’, ‘B’, ‘C’, ‘A’ are inserted into a queue (in that order), and then removed one at
a time from the queue, in what order will they be removed?
a. ACBD
b. ABCD
c. DBCA
d. DCAB
11) Which of the following is an accurate Big-Oh characterization of the running time of the linear
search algorithm if the search pool is not sorted? Assume that n is the size of the search pool.
a. O(log2n)
b. O(nlog2n)
c. O(n)
d. None of the above
12) Which of the following is an accurate Big-Oh characterization of the running time of the following
recursive method?
public static int foo(int N) {
if(N==1 || N==0) return 1;
else return foo(N/2);}
a. O(N)
b. O(N2)
c. O(Nlog2(N))
d. O(log2(N))
13) Which of the following is an accurate Big-Oh characterization of the running time of the following
recursive method?
public static void foo(int N) {
if(N==0) return;
else {
foo(N-1);
System.out.println(“Move”);
foo(N-1);
}}
a. O(2N)
b. O(N2)
c. O(Nlog2(N))
d. None of the above
14) Which of the following has a time complexity of O(log2(n)) with n being the size of the data
structure?
a. The contains method of the TreeSet class
b. The remove method of the PriorityQueue class
c. Both of the above
d. None of the above
15) Which of the following functions of N has the highest asymptotic growth rate?
a. N3
b. N
c. log2N
d. N2log2N
4
2. 2𝑛 + 2 is 𝜃(𝑛) (Big-theta)
Answer: True False
3. The following recursive implementation of the sum function has constant space complexity; that is,
it has a space usage of O(1).
public int sum(int n) {
if(n == 0) return 0;
else return 1 + sum(n-1);
}
Answer: True False
4. The Fibonacci series begins with 0 and 1 and has the property that every subsequent Fibonacci
number is the sum of the three preceding values.
Answer: True False
5. The Stack ADT we developed in class included the following method names: pop, push, peek,
size, and isEmpty.
Answer: True False
6. The recursive solution of the binary search problem, which we developed in class, has logarithmic
time complexity.
Answer: True False
7. The values stored in the Java built-in PriorityQueue class are both unique and sorted.
Answer: True False
8. Consider a version of the “Josephus Problem” that has n players. Assume that the leader waits until
the token has been passed around k times before ringing the bell. The queue-based solution we
developed in class for this problem has a time complexity of O(nk) and a space complexity of
O(k).
Answer: True False
9. The java.util.Stack built-in class offered by the Java language provides a method called top
that returns the top element of the stack.
Answer: True False
10. Consider the ArrayBasedStack implementation we developed in class. In the context of this
implementation, the size of the stack represents the total number of non-null values stored in the
stack while the capacity of the stack is the maximum number of values that the stack can hold.
Answer: True False
5
import java.util.*;
public int product(int a, int b) {
if(a > b) {
return product(b, a);
}
int smaller = a, bigger = b;
if(smaller == 0) return 0;
else if(smaller == 1) return bigger;
int s = smaller/2;
int halfProd = product(s, bigger);
if(smaller % 2 == 0) {
return halfProd + halfProd;
} else {
return halfProd + halfProd + bigger;
}
}
6
Building on the above, “”, “()()”, and “()(()())” are VPSs with nesting depths of 0, 1, and 2,
respectively. On the other hand, “(()” is not a VPS.
Write a function called maxDepth that accepts a String S representing a VPS. Your function should
return the nesting depth of S.
Example 1:
Input: (1+(2*3)+((8)/4)+1)
Output: 3
Explanation: Digit 8 is inside of 3 nested parentheses
Example 2:
Input: (1)+((2))+(((3)))
Output: 3
Explanation: Digit 3 is inside of 3 nested parentheses
Example 3:
Input: 1
Output: 0
Constraints:
1 <= S.length() <= 100
S consists of digits 0-9 and the characters ‘+’, ‘-’, ‘*’, ‘/’, ‘(’, and ‘)’
It is guaranteed that S is a VPS
7
import java.util.*;
public int maxDepth(String s) {
if(s.length() == 1) {
return 0;
}
int max = 0;
Stack<Character> stack = new Stack<>();
return max;
8
}
9