Data Structures and Algorithms (NTU, Class 01/02, Spring 2014) instructor: Hsuan-Tien Lin
Midterm Examination Problem Sheet
TIME: 04/13/2013, 14:00–16:00
This is a open-book exam. You can use any printed materials as your reference during the exam. Any
electronic devices are not allowed.
Any form of cheating, lying or plagiarism will not be tolerated. Students can get zero scores and/or get
negative scores and/or fail the class and/or be kicked out of school and/or receive other punishments for
those kinds of misconducts.
Both English and Chinese (if suited) are allowed for answering the questions. We do not accept any
other languages.
There are 8 problems in the exam, each worth 25 points—the full credit is 200 points. Some problems
come with a few sub-problems to help you get partial credits. For the 8 problems, 3 of them are marked
with * and are supposedly simple; 3 of them are marked with ** and are supposedly regular; 2 of them
are marked with *** and are supposedly difficult. There is one bonus problem, numbered 1126, with 10
points. The problems are roughly ordered by difficulty.
1. (25%, *) Considering representing a deque by a doubly-linked list, with an empty deque in the
beginning. After executing each step of the following operations sequentially, list the contents of
the doubly-linked list, with highlights on where the head and tail are.
insertFront(1); insertBack(3); insertBack(2); insertFront(5); removeFront();
removeBack(); insertBack(7); removeFront(); removeBack(); insertBack(6);
2. (25%, *) The following is a file system that can be represented by a tree rooted at dsa. Draw
a diagram illustrating how the tree can be stored when the sub-trees are put in a linked-list-based
container. You only need to draw links from parent to child as well as links between siblings, but
not other links.
dsa/instructor
dsa/ta/ta1
dsa/ta/ta2
dsa/ta/ta3
dsa/hw/hw1/write
dsa/hw/hw1/prog
dsa/hw/hw2/write
dsa/hw/hw2/prog
dsa/hw/hw2/data
dsa/midterm/goodluck
1 of 3
Data Structures and Algorithms (NTU, Class 01/02, Spring 2014) instructor: Hsuan-Tien Lin
3. (25%, *) Write down the outputs of the following C++ code.
(a) (15%) Execute max(arr, 0, 6, res) with an array arr = {1, 2, 3, 4, 5, 6, 7} and res = 0.
1 void max( int ∗ a r r , int l e f t , int r i g h t , int& r e s ) {
2 int mid = ( l e f t +r i g h t ) / 2 ;
3 int r e s 1 = 0 ;
4 int r e s 2 = 0 ;
5 i f ( l e f t == r i g h t ) {
6 r e s = a r r [ mid ] ;
7 }
8 else {
9 max( a r r , l e f t , mid , r e s 1 ) ;
10 max( a r r , mid+1, r i g h t , r e s 2 ) ;
11 s t d : : c o u t << r e s 1 << ’ ’ << r e s 2 << ’ ’ << s t d : : e n d l ;
12 res = ( res1 > res2 ? res1 : res2 ) ;
13 }
14 }
(b) (10%) Execute max(arr, 0, 6, res) with an array arr = {1, 2, 3, 4, 5, 6, 7} and res = 0.
1 void max( int ∗ a r r , int l e f t , int r i g h t , int r e s ) {
2 int mid = ( l e f t +r i g h t ) / 2 ;
3 int r e s 1 = 0 ;
4 int r e s 2 = 0 ;
5 i f ( l e f t == r i g h t ) {
6 r e s = a r r [ mid ] ;
7 }
8 else {
9 max( a r r , l e f t , mid , r e s 1 ) ;
10 max( a r r , mid+1, r i g h t , r e s 2 ) ;
11 s t d : : c o u t << r e s 1 << ’ ’ << r e s 2 << ’ ’ << s t d : : e n d l ;
12 res = ( res1 > res2 ? res1 : res2 ) ;
13 }
14 }
4. (25%, **) Consider having two stacks for storing integers, a blue one and a green one with the
push and pop operations only.
(a) (15%) Illustrate a scheme (with code or figure) that uses the two stacks and at most O(1)
of additional memory to simulate THREE stacks, numbered 1, 2, 3. In particular, illustrate
how to implement the push(int stack number, int value) and pop(int stack number)
operations clearly.
(b) (10%) Illustrate a scheme (with code or figure) that uses the two stacks and at most O(1) of
not-in-stack memory to simulate K stacks, numbered 1, 2, 3, . . ., K. Note that the amount
of not-in-stack memory that you can use should be independent of K.
2 of 3
Data Structures and Algorithms (NTU, Class 01/02, Spring 2014) instructor: Hsuan-Tien Lin
5. (25%, **) Consider mathematical k-dimensional vectors stored with a sparse array on the list.
That is, a vector v = (3, 0, 0, 0, 9, 6) is stored as an ordered list of (0, 3) → (4, 9) → (5, 6). Use any
pseudo-code to describe an algorithm that computes the squared distance between two vectors v
Pk−1
and u. That is, i=0 (v[i] − u[i])2 . The algorithm should run in time complexity linear to the
number of non-zero terms in v and u. Briefly justify how your algorithm achieves such a time
complexity.
6. (25%, **) In Problems 6 and 7, the notation f (n) = O(g(n)) applies to functions f (n) and g(n)
from N to R+ ∪ {0}, and f (n) = O(g(n)) if and only if there exists n0 > 0 and c > 0 such that
∀n ≥ n0 , f (n) ≤ cg(n).
(a) (15%) Assume that k ∈ N. Let f (n) = log2 (1+|ak nk +ak−1 nk−1 +ak−2 nk−2 +· · ·+a0 |),
and gk (n) = log2 nk . Prove that f (n) = O(gk (n)). You can use the fact that 1 + |ak nk +
ak−1 nk−1 + ak−2 nk−2 + · · · + a0 | = O(nk ), as well as the definition of O(·) above. But you
cannot use any other theorems or claims beyond high-school math unless you prove them first.
(b) (10%) Let f (n) = log2 (1 + |ak nk + ak−1 nk−1 + ak−2 nk−2 + · · · + a0 |), and g(n) = log2 n.
Prove that f (n) = O(g(n)). You can use the result from the previous sub-problem (even if
you don’t prove it) if you want.
7. (25%, ***) Consider a non-decreasing function h(m) : R+ ∪{0} → R+ ∪{0}, which means h(m0 ) ≥
h(m) for all m0 ≥ m. Prove or disprove the following statement. “For functions f (n) and g(n)
from N to R+ ∪ {0}, if f (n) = O(g(n)), then h(f (n)) = O(h(g(n))).” (Hint: this appears to be a
more general statement that can be used to prove Problem 6(a). But is it still true? )
8. (25%, ***) Consider a consecutive array of capacity N that holds k non-null elements, where
0 ≤ k ≤ N . That is, all non-null elements are in positions 0, 1, 2, . . ., k − 1, while all other
positions contain null. Assume that you accidentally lose the value k and all you know is N
and the array. Describe an O(log N )-time algorithm that computes k. Briefly discuss why the
algorithm is correct and how it achieves such a time complexity. (Hint: think about binary search.)
1126. (Bonus 10%, ???) You learned about selection sort in a very early stage of this class, and you
learned about insertion sort in your reading assignment. Now, imagine that you are in the industry
and your boss asks you to implement a sorting algorithm on an array by yourself—without using
any of the existing implementations such as qsort. Would you choose selection sort or insertion
sort? What is the reason that you’d use to convince your boss that you are making a good choice?
(Hint: Imagine that your boss, who is not of CS major, asks you this question. The TAs will
grade qualitatively on whether your reason is correct and whether you can convince your boss in a
reasonable manner.)
3 of 3