Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

CE 221 Data Structures and Algorithms

Potential Midterm-like Questions

Instructions
• Please write your solutions in the spaces provided on the exam sheets. Make sure your
solutions are neat and clearly marked. You may use the blank areas of the exam pages for
scratch work. Please do not use any additional scratch paper.

• Simplicity and clarity of solutions do count. You may get as few as zero points for a
problem if your solution is far more complicated than necessary, or if we cannot understand
your solution.

Duration: 100 minutes

Problem 1 2 3 4 5 Total
Max. Points 20 15 20 25 20 100
Score

Good Luck

1/4
Questions

Problem 1) Asymptotic Complexity Analysis Matters [20 pts.]


Write a recursive function that returns the number of 1’s in the binary
representation of N. Use the fact that this is equal to the number of ones in the
representation of N/2, plus 1, if N is odd. Note that you are allowed to use a pseudo
code representation for writing the function. What is the asymptotic running time
complexity of your function? Specify it in big-Oh notation by writing down and then
solving the corresponding recurrence relation.
Solution:

Problem 2) Linked Lists with Sentinels [15 pts.]


Suppose that a singly linked list is implemented with both a header and a tail node.
These sentinel nodes do not contain data, but are used to mark the beginning and the
end of the list. Describe a constant time (i.e., O (1)) algorithm to Remove the item
stored at the node referenced by p. Note that it is not allowed to remove neither the
header nor the tail node. Please do not forget 1) to present the algorithm clearly in
pseudo-code and 2) account briefly for its running time complexity in big-Oh notation.
Hint: Consider copying the data for p’s next node to the node p, and then delete the
next node.
Solution:

2/4
Problem 3) Efficiency is the major concern! [20 pts.]
In this question, you are to design an efficient algorithm that takes an array of
positive integers named A with size n, and return the maximum value of A[j]-A[i],
with n > j ≥ i ≥ 0. Your algorithm should run in O (n). You can assume that the
function prototype for your algorithm is given by int maxDiff(int A[],int n).
Hint: Try to beat the maximum found while scanning the array. At any point in the
algorithm, while j scans the array, i refers to the position of the minimum value seen
so far.
Solution:

Problem 4) Binary Search Trees [25 pts.]


4.1 [15 pts.] Show the result of inserting 3, 1, 4, 6, 9, 2, 5, 7 in exactly this order
into an initially empty binary search tree. Note that you do not need to illustrate the
intermediate steps.
Solution:

4.2 [10 pts.] Show the result of deleting the root of the binary search tree formed
after the insertions performed above (i.e., perform the deletion in the tree obtained
in 4.1).
Solution:

3/4
Problem 5) Expression Trees and Tree Traversals [20 pts.]
Give the inorder, postorder, and preorder expressions corresponding to the
expression tree shown below.

* e

* +

a b c d

Solution:

Good Luck

4/4

You might also like