0% found this document useful (0 votes)
4 views9 pages

Exam II - Fall21 (2nd Section) - Sol

This document is an exam for COE 312 – Data Structures, administered by Dr. Wissam F. Fawaz on November 25, 2021. It includes multiple-choice questions, true or false questions, and coding problems related to data structures and algorithms. Students are instructed to complete the exam in 85 minutes, with specific guidelines on answering and submission.

Uploaded by

obeid2.khaled
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views9 pages

Exam II - Fall21 (2nd Section) - Sol

This document is an exam for COE 312 – Data Structures, administered by Dr. Wissam F. Fawaz on November 25, 2021. It includes multiple-choice questions, true or false questions, and coding problems related to data structures and algorithms. Students are instructed to complete the exam in 85 minutes, with specific guidelines on answering and submission.

Uploaded by

obeid2.khaled
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

1

COE 312 – Data Structures

Welcome to Exam II (2nd Section)


Thursday November 25, 2021

Instructor: Dr. Wissam F. Fawaz

Name: _______________________

Student ID: ________________

Instructions:

1. This exam is Closed Book. Please do not forget to write your


name and ID on the first page.
2. You have exactly 85 minutes to complete the 4 required
problems.
3. Read each problem carefully. If something appears ambiguous,
please write your assumptions.
4. Points allocated to each problem are shown in square brackets.
5. Put your answers in the space provided only. No other spaces
will be graded or even looked at.

Good Luck!!
2

Problem 1: Multiple Choice Questions (20 minutes) [25 points]


1) The space complexity of the recursive merge sort algorithm is best described as
a. constant
b. linear
c. logarithmic
d. exponential
2) Consider the following method definition that is supposed to return the sum of the int values from
1 (inclusive) to x (inclusive)
public int sum(int x) {
if(x==0)
return 1;
else return sum(x-1) + x;}
What is wrong with this recursive implementation?
a. The recursive part should return sum(x-1) + 1; instead of sum(x-1) + x;
b. The base case should return 0 instead of 1
c. The method should return a boolean instead of an int
d. There is nothing wrong with the recursive implementation
3) Which of the following is Ω(√𝑛)?
a. log2(n)
b. n
c. Both of the above
d. None of the above
4) Give a Big-Oh characterization for the running time of the following pseudo-code fragment
x = 0
for i = 1 to 10000 do
for j = 1 to 10000 do
x = x + 1
a. O(1)
b. O(n)
c. O(n2)
d. O(2n)
5) Consider a stack data structure that is realized via an array along with a variable called top
representing the index of the top element of the stack, similarly to the implementation we developed
in class. Which of the following can be used to find the size of the stack?
a. top
b. top+1
c. top+2
d. top-1
6) Consider an array-based implementation of a queue that is neither drifting nor circular. If the rear
element of the queue is always stored at index 0, then the enqueue operation requires
a. O(1) time
b. O(log2(n)) time, with n being the capacity of the array
c. O(n) time, with n being the capacity of the array
d. None of the above is an accurate Big-Oh characterization of the running time of enqueue
7) Which of the following is the most efficient in terms of its running time?
a. Merge sort algorithm
b. Priority queue sort algorithm
c. Insertion sort algorithm
d. Both (a) and (b) are equally efficient and are more efficient than insertion sort in terms of
running time
8) Which of the following is not a method of the java.util.Collections class?
a. reverseOrder
b. sort
c. reverse
d. dijoint
3

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

Problem 2: True or False questions (10 minutes) [20 points]


1. The dequeue() method of the ArrayBasedQueue we developed in class both removes and
returns the front element of the queue.
Answer: True False

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

Problem 3: Coding problem I (25 minutes) [25 pts]


Write a recursive function to multiply two positive integers without using the multiplication operator (i.e.,
the “*” symbol). You can use the addition operator (+), subtraction operator (-), or even the division operator
in your solution. But, you should strive to minimize the number of operations performed.
Providing an iterative solution for this problem would result in the loss of 10 points (Note that an iterative
solution can use the multiplication sign. However, a valid iterative solution is one that uses a repetition
statement; so, returning num1*num2 will not be considered as an iterative solution).
Hint for optimal recursive solution:
num1 = 30, num2 = 35
product(30,35)=2*product(15,35) (Cut smaller parameter in half)
product(15,35)=2*product(7,35)+35 (Cut smaller in half & add larger as smaller is odd)
product(7,35)=2*product(3,35)+35
product(1,35)=35

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

Problem 4: Coding Problem II (30 minutes) [30 pts]


A string is a Valid Parentheses String (denoted by VPS) if it satisfies one of the following properties:
 It is an empty string “”, or a single character not equal to “(” or “)”,
 It can be written as AB (A concatenated with B), where A and B are each a VPS (i.e., the combination
of two VPSs results in a VPS),
 It can be written as (A), where A is a VPS (i.e., surrounding a VPS with a pair of parentheses yields
a VPS).
We can similarly define the nesting depth function, depth(S), of any VPS S as follows:
 depth(“”) = 0
 depth(C) = 0, where C is a string having a single character that is not equal to “(” or “)”
 depth(A + B) = max(depth(A), depth(B)), where A and B are VPSs.
 depth(“(” + A + “)”) = 1 + depth(A), where A is a VPS.

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<>();

for(char letter : s.toCharArray()) {


if(letter == '(') {
stack.push(letter);
} else if(letter == ')') {
max = Math.max(max, stack.size());
stack.pop();
}
}

return max;
8

}
9

Appendix: Classes and Methods


java.util.HashMap<K,V> java.util.LinkedList<T>
HashMap() LinkedList()
Set<K> keySet() boolean add(T)
boolean containsKey(K) void addFirst(T)
V get(K) void addLast(T)
V put(K, V) int size()
V remove(K) boolean addAll(Collection)
java.util.TreeMap<K,V> ListIterator<T> listIterator(int)
TreeMap() List<T> subList(int,int)
Set<K> keySet() java.util.ListIterator<E>
boolean containsKey(K) boolean hasNext()
V get(K) boolean hasPrevious()
V put(K, V) E next()
V remove(K) E previous()
K firstKey() void set(E)
java.util.Comparator<T> java.util.HashSet<T>
boolean compare(T o1, T o2) HashSet(Collection)
java.util.PriorityQueue<T> HashSet()
PriorityQueue() boolean add(T e)
PriorityQueue(Comparator) boolean contains(T)
boolean contains(T) boolean remove(T)
boolean add(T) int size()
boolean remove() java.util.TreeSet<T>
T peek() TreeSet(Collection)
int size() TreeSet()
java.util.Collections TreeSet(Comparator)
static : boolean contains(T)
void sort(List<T>) TreeSet headSet(T)
T min(Collection<T>) TreeSet tailSet(T)
T max(Collection<T>) T first()
void reverse(List<T>) T last()
void copy(List, List) int size()
java.util.Stack<T>
boolean isEmpty()
T peek() throws EmptyStackException
T pop() throws EmptyStackException
T push(T e)

You might also like