Docsity

Final Exam Practice Problems Solutions - Computer Science II | COP 3503, Exams of Computer Science

Toggle sidebar
bg1
COP 3503 โ€“ Final Exam Practice Problems - SOLUTIONS
1. For the binary tree shown below, show the output of a preorder traversal of the tree. For
additional practice do: inorder, postorder, and level order traversals.
Preorder โ€“ A, B, C, D, E, F, G, H, I
Additional Practice:
Inorder โ€“ B, D, C, A, F, H, G, I, E
Postorder โ€“ D, C, B, H, I, G, F, E, A
Levelorder - A, B, E, C, F, D, G, H, I
2. Define and explain the following terms:
(1) a greedy algorithm โ€“
a locally optimal decision is made by the algorithm without regards to the future
consequences or overall optimality of the decision made at that point.
(2) divide and conquer algorithm
a recursive algorithm that consists of two parts: (1) recursively dividing the problem
into smaller and smaller subproblems ultimately arriving at the base case, and (2) the
solution of the original problem is formed from the solutions to the subproblems.
3. Show the contents of a stack after each of the following operations. What is the final value of the
stack?
(1) push (20)
(2) push (15)
(3) push (pop( ) - pop( ))
(4) push (10)
(5) push( pop( ) + pop( ))
(6) top( )
stack = [20]
stack = [15, 20]
stack = [๏€ญ5]
stack = [10,๏€ญ 5]
stack = [5]
return 5
1
A
B E
C F
GD
H I
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Final Exam Practice Problems Solutions - Computer Science II | COP 3503 and more Exams Computer Science in PDF only on Docsity!

COP 3503 โ€“ Final Exam Practice Problems - SOLUTIONS

1. For the binary tree shown below, show the output of a preorder traversal of the tree. For

additional practice do: inorder, postorder, and level order traversals.

Preorder โ€“ A, B, C, D, E, F, G, H, I

Additional Practice:

Inorder โ€“ B, D, C, A, F, H, G, I, E

Postorder โ€“ D, C, B, H, I, G, F, E, A

Levelorder - A, B, E, C, F, D, G, H, I

2. Define and explain the following terms:

(1) a greedy algorithm โ€“

a locally optimal decision is made by the algorithm without regards to the future

consequences or overall optimality of the decision made at that point.

(2) divide and conquer algorithm

a recursive algorithm that consists of two parts: (1) recursively dividing the problem

into smaller and smaller subproblems ultimately arriving at the base case, and (2) the

solution of the original problem is formed from the solutions to the subproblems.

3. Show the contents of a stack after each of the following operations. What is the final value of the

stack?

(1) push (20)

(2) push (15)

(3) push (pop( ) - pop( ))

(4) push (10)

(5) push( pop( ) + pop( ))

(6) top( )

stack = [20]

stack = [15, 20]

stack = [ ๏€ญ 5]

stack = [10, ๏€ญ 5]

stack = [5]

return 5

A

B E

C F

D G

H I

4. Shown below is an expression tree that represents a valid infix expression. Convert the expression

to a prefix expression. What technique did you use to do this conversion? For additional practice

assume that you want the postfix form of the expression from the tree.

prefix expression is: / * c + a b * e โ€“ f g

technique is a prefix traversal of the tree

Additional practice: postfix traversal of the tree give the postfix expression: c a b + * e f g - * /

5. Write a pseudocode algorithm that will delete an arbitrary node identified by the pointer del from a

doubly linked list.

function delete (list, del)

{ // list is the doubly-linked list, del points to the node to delete

// list node contains data field and two pointer fields: prev and next

behind : pointer;

behind = del.prev;

del.next.prev = behind;

behind.next = del.next;

free(del); //return node to the heap (optional)

c

+ e -

a

f g

b

7. Consider the linked lists shown below. Connect the new node into its proper place within the

existing list. What are the three lists representing? What is the Big-Oh time complexity of this

operation?

in2 in3 null null

in

null

newnode

List represents a list of odd numbers in ascending order

List represents a list of even numbers in ascending order

List represents a list of prime numbers in ascending order

Big-Oh time complexity is: in general you must โ€œfindโ€ the correct location to insert the new node in

three different lists โ€“ thus 3n = O(n) where n is the number of nodes in the list.

8. An algorithm takes 0.5 msec to run on an input set of size 100. How long will the algorithm take to

run if the input size is 500 and the algorithm is (a) cubic, (b) quadratic, and (c) linear?

(a) cubic algorithm: T(N) = cN^3 , thus T(5N) = c(5N)^3 ๏ƒž T(5N) = 125cN^3 = 125T(N) therefore

T(500) = 125(0.5๏‚ด 10 -3^ sec) = 0.0625 seconds (62.5 msec)

(b) quadratic algorithm: T(N) = cN^2 , thus T(5N) = c(5N)^2 ๏ƒž T(5N) = 25cN^2 = 25T(N) therefore

T(500) = 25(0.5๏‚ด 10 -3^ sec) = 0.0125 seconds (12.5 msec)

(c) linear algorithm: T(N) = cN, thus, T(5N) = c(5N) ๏ƒž T(5N) = 5cN = 5T(N) therefore T(500) =

5(0.5๏‚ด 10 -3^ sec) = 0.0025 seconds (2.5 msec)

9. Given the array shown below, show the results of each pass of a Shell sort on the array

assuming that that initial gap is 5 (i.e., do a 5-sort first) and only odd numbered gaps are used.

index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

value 8 5 35 23 14 9 2 6 33 1 16 17 24 27 12 40 13

work area

5-sort 8 5 35 23 14 9 2 6 33 1 16 17 24 27 12 40 13 8 2 6 23 1 9 5 24 27 12 16 13 35 33 14 40 17 end 5- sort 8 2 6 23 1 9 5 24 27 12 16 13 35 33 14 40 17 3-sort 8 2 6 23 1 9 5 24 27 12 16 13 35 33 14 40 17 5 1 6 8 2 9 12 16 13 23 17 14 35 24 27 40 33 end 3- sort

1-sort 1 5 1 5 6 1 5 6 8 1 2 5 6 8 1 2 5 6 8 9 1 2 5 6 8 9 12 1 2 5 6 8 9 12 16 1 2 5 6 8 9 12 13 16 1 2 5 6 8 9 12 13 16 23 1 2 5 6 8 9 12 13 16 17 23 1 2 5 6 8 9 12 13 14 16 17 23 1 2 5 6 8 9 12 13 14 16 17 23 35 1 2 5 6 8 9 12 13 14 16 17 23 24 35 1 2 5 6 8 9 12 13 14 16 17 23 24 27 35 1 2 5 6 8 9 12 13 14 16 17 23 24 27 35 40 sorted 1 2 5 6 8 9 12 13 14 16 17 23 24 27 33 35 40

Divide and conquer.

(b) Assuming that the overhead operations required by this algorithm are linear, what is the Big-Oh

running time of this multiplication algorithm?

Number of subproblems to be solved, A = 3 (there are 3 recursive calls in the algorithm).

Size of the subproblems, B = 2 (each integer is broken into 2 halves).

Overhead is linear so, k = 1

To determine the running time of this divide and conquer algorithm we have:

A? Bk^ ๏ƒž 3 > 2^1 ๏ƒž O(NlogBA) = O(Nlog 23 ) = O(N1.59)

(c) Does this algorithm run faster than the technique you were taught in grade school? If it does,

what might be a reason that you werenโ€™t taught this algorithm in grade school?

One reason is the complexity of the technique โ€“ 2nd^ and 3rd^ graders might not understand the

technique! Another reason is that we have ignored constants of proportionality. While the

algorithm is assymptotically superior to the partial product technique, the constants are such that

for numbers with less than about 500 bits, the grade school technique is faster!

13. Produce both a depth-first and a breadth-first traversal of the following tree. Show the contents

of the supporting data structures (at each step) as well.

Solutions:

a 5

e

m r s

b

1 3 f 1

n 1

t

x

Depth-First Traversal requires an additional stack. This is shown with the format: [ top โ€ฆ bottom]

1. [6]

2. visit 6, push children [ 7, a, 5, 4, 13]

3. visit 7, push children [ 1, 2, 3, a, 5, 4, 13]

4. visit 1, no push โ€“ no children [2, 3, 1, 5, 4, 13]

5. visit 2, push children [m, n, 3, 1, a, 5, 4, 13]

6. visit m, no push โ€“ no children [n, 3, 1, a, 5, 4, 13]

7. visit n, push children [x, 3, 1, a, 5, 4, 13]

8. visit x, no push โ€“ no children [3, 1, a, 5, 4, 13]

9. visit 3, no push โ€“ no children [1, a, 5, 4, 13]

10. visit 1, no push โ€“ no children [a, 5, 4, 13]

11. visit a, no push โ€“ no children [5, 4, 13]

12. visit 5, push children [e, 4, 13]

13. visit e, push children [b, 4, 13]

14. visit b, no push โ€“ no children [4, 13]

15. visit 4, push children [f, 13]

16. visit f, push children [11, 13]

17. visit 11, no push โ€“ no children [13]

18. visit 13, push children [10, 9, 8, 7]

19. visit 10, push children [r, s, 9, 8, 7]

20. visit r, push children [14, s, 9, 8, 7]

21. visit 14, no push โ€“ no children [s, 9, 8, 7]

22. visit s, no push โ€“ no children [9, 8, 7]

23. visit 9, no push โ€“ no children [8, 7]

24. visit 8, no push โ€“ no children [7]

25. visit 7, push children [t]

26. visit t, no push โ€“ no children

DONE

Output order: 6, 7, 1, 2, m, n, x, 3, 1, a, 5, e, b, 4, f, 11, 13, 10, r, 14, s, 9, 8, 7, t

Breadth-First Traversal requires an additional queue. This is shown with the format: [head โ€ฆ tail]

1. [6]

2. visit 6, enqueue children [7, a, 5, 4, 13]

3. visit 7, enqueue children [a, 5, 4, 13, 1, 2, 3]

4. visit a, no children โ€“ no enqueue [5, 4, 13, 1, 2, 3]

5. visit 5, enqueue children [4, 13, 1, 2, 3, e]

6. visit 4, enqueue children [13, 1, 2, 3, e, f]

7. visit 13, enqueue children [1, 2, 3, e, f, 10, 9, 8, 7]

8. visit 1, no children โ€“ no enqueue [2, 3, e, f, 10, 9, 8, 7]

9. visit 2, enqueue children [3, e, f, 10, 9, 8, 7, m, n]

10. visit 3, no children โ€“ no enqueue [e, f, 10, 9, 8, 7, m, n]

11. visit e, enqueue children [f, 10, 9, 8, 7, m, n, b]

12. visit f, enqueue children [10, 9, 8, 7, m, n, b, 11]

13. visit 10, enqueue children [9, 8, 7, m, n, b, 11, r, s]

14. visit 9, no children โ€“ no enqueue [8, 7, m, n, b, 11, r, s]

15. visit 8, no children โ€“ no enqueue [7, m, n, b, 11, r, s]

Determine the length of the period of the sequence of random numbers generated by this

algorithm assuming the values, A = 5, X 0 = 1, and M = 11. Show your work at each step and

show every value in the period of the sequence as well as length of the period. Does this value of

A provide a full period?

X 0 = 1

X 1 = 5(1)[mod 11] = 5

X 2 = 5(5)[mod 11] = 4

X 3 = 5(4)[mod 11] = 9

X 4 = 5(9)[mod 11] = 1 - sequence repeats

Sequence is 1, 5, 4, 9

Period is 4

No, this is not a full period.

16. Given the two functions below, determine which is asymptotically bigger? Show your

justification.

f(n) = 4n^3 + 3n^2 + 5n + 2 g(n) = 24n^2 + 35n + 10

divide both functions by n^3 (to reduce dominant term to a constant) which will produce:

Thus, 4n^3 + 3n^2 + 5n + 2 is asymptotically bigger than 24n^2 + 35n + 10.

Similarly, 24n^2 + 35n + 10 is asymptotically smaller than 4n^3 + 3n^2 + 5n + 2.

4 n 3 n 5 n 2

24 n 35 n 10

lim 32

2

n ๏€ซ ๏€ซ ๏€ซ

๏‚ฎ ๏‚ฅ

4 3 /n 5 /n 2 / n

24 /n 35 /n 10 / n

2 3 2 3

Alternate view of the solution for Problem #9.

index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

value 8 5 35 23 14 9 2 6 33 1 16 17 24 27 12 40 13

5-sort 8 9 16 40 5 2 17 13 35 6 24 23 33 27 14 1 12 5- sorted

3-sort 8 23 5 12 35 40 2 1 24 16 33 17 6 9 27 13 14 3- sorted 5 1 6 8 2 9 12 16 13 23 17 14 35 24 27 40 33 1-sort 1 5 1 5 6 1 5 6 8 1 2 5 6 8 1 2 5 6 8 9 1 2 5 6 8 9 12 1 2 5 6 8 9 12 16 1 2 5 6 8 9 12 13 16 1 2 5 6 8 9 12 13 16 23 1 2 5 6 8 9 12 13 16 17 23 1 2 5 6 8 9 12 13 14 16 17 23 1 2 5 6 8 9 12 13 14 16 17 23 35 1 2 5 6 8 9 12 13 14 16 17 23 24 35 1 2 5 6 8 9 12 13 14 16 17 23 24 27 35 1 2 5 6 8 9 12 13 14 16 17 23 24 27 35 40 sorted 1 2 5 6 8 9 12 13 14 16 17 23 24 27 33 35 40