Practice Midterm Exam 1 - Algorithms and Data Structures | CS 200, Exams of Computer Science

Material Type: Exam; Professor: Pallickara; Class: Algorithms and Data Structures; Subject: Computer Science; University: Colorado State University; Term: Fall 2011;

Typology: Exams

2010/2011

Uploaded on 12/29/2011

boldstepdesign
boldstepdesign 🇺🇸

5

(1)

5 documents

1 / 26

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Page 1 of 26!
CS#200#Algorithm#and#Data#Structure##################Midterm#Exam#I#
Problem#1.#!Multiple#Choice#Questions:#20#pts#(4#pts#x#5)#
#
1) If!the!array:!
6,!21,!35,!3,!6,!2,!13!
is!added!to!a!stack,!in!the!order!given!above,!which!of!the!following!is!the!top!of!the!
stack?!
a) 2!!!!!!!!!!!b)!6!!!!!!!!!!!!c)!3!!!!!!!!!!!d)!13!!!!!!!!!!!e)!35!
!
!
!
!
!
2) What!is!the!value!of!the!prefix!expression:!!*!3!8!+!6!5?!!
a) 11!!!!!!!!!!b)!23!!!!!!!!!!!c)!13!!!!!!!!!!!d)!21!
!
!
!
3) The!correct!grammar!for!the!following!language!L:!
L!=!{w:!w!is!of!the!form!SnDn!for!some!n!≥!0}!
is!______.!!
a) <legalTword>!=!S!|!<legalTword>!|!D!
b) <legalTword>!=!empty!string!|!S!|!<legalTword>!|!D!
c) <legalTword>!=!S!<legalTword>!D!
d) <legalTword>!=!empty!string!|!S!<legalTword>!D!
!
4) A! growthTrate! function! of! ______! implies! a! problem! whose! time! requirement! is!
constant.!
a) 1!
b) n!
c) 2n!
d) log2n!
!
!
!
5) The!value!of!which!of!the!following!growthTrate!functions!grows!the!fastest?!
a) O(n)!
b) O(n2)!
c) O(1)!
d) O(log2n)!!!
!
!
!
!
!
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a

Partial preview of the text

Download Practice Midterm Exam 1 - Algorithms and Data Structures | CS 200 and more Exams Computer Science in PDF only on Docsity!

CS 200 Algorithm and Data Structure Midterm Exam I

Problem 1. Multiple Choice Questions: 20 pts (4 pts x 5)

  1. If the array: 6, 21, 35, 3, 6, 2, 13 is added to a stack, in the order given above, which of the following is the top of the stack? a) 2 b) 6 c) 3 d) 13 e) 35
  2. What is the value of the prefix expression: – * 3 8 + 6 5? a) 11 b) 23 c) 13 d) 21
  3. The correct grammar for the following language L: L = {w: w is of the form SnDn^ for some n ≥ 0} is ______. a) <legal-­‐word> = S | <legal-­‐word> | D b) <legal-­‐word> = empty string | S | <legal-­‐word> | D c) <legal-­‐word> = S <legal-­‐word> D d) <legal-­‐word> = empty string | S <legal-­‐word> D
  4. A growth-­‐rate function of ______ implies a problem whose time requirement is constant. a) 1 b) n c) 2 n d) log 2 n
  5. The value of which of the following growth-­‐rate functions grows the fastest? a) O(n) b) O(n^2 ) c) O(1) d) O(log 2 n)

Problem 2. Simple Questions: 15 pts (5pts x 3)

  1. What is the action performed by the peek operation in a stack?
  2. Suppose we begin with an empty stack, and perform the following operations: push 7, push 2, push 9, push 6, pop, pop, peek, push 1, push 3, peek, push 8, pop, peek, pop, pop, push 5, push 4, pop, pop, pop, push 8. What is contained on the stack when we are done? Write out the contents from top to bottom.

Problem 3. 10 pts (5 pts x 2)

Consider the language that the following grammar defines: < S > = # | < W > | #< S > < W > = ab | a < W > b

  1. Write all strings that are in this language and that contain five or fewer characters.
  2. Is the string #aabbb in this language?

Problem 4. 10 pts

Use mathematical induction to show that, 1 + 2 + 2^2 + 2^3 + 2^4 + … + 2n^ = 2n+1^ – 1 for all nonnegative integer n.

Problem 6. 15 pts (3pts x 5)

Give as good a Big O estimate as possible for each of these functions. (1) f(n ) = ( n^2 + 8)( n + 1) (2) f ( n ) = ( n log 2 n + n^2 )( n^3 +2) (3) f(n ) = ( n^2 + 1) / ( n + 1) (4) f ( n ) = 10 (5) f ( n ) = 7 * log 2 n * n

Problem 7. 10 pts

Show that x^2 + 4 x + 17 is O ( x^3 ) but that x^3 is not O ( x^2 + 4 x + 17 ). ( Hint : Use the definition of Big O estimation)

Problem 9. 10pts (3+3+4)

Consider the following Java method f. 1: public static void f(int[] theArray, int n){ 2: int temp; 3: for (int j = 0; j < n; ++j){ 4: int i = 0; 5: while (i <=j) { 6: if (i < j){ 7: temp = theArray[i]; 8: theArray[i] = theArray[j]; 9: theArray[j] = temp; 10: } 11: ++i; 12: } 13: } 14:} (1) How many comparisons does f perform? (We consider line 6 as the only comparison) (2) How many data movement does f perform? (We do not consider additions or initializations of integer variables as data movement.) (3) What is the Big O estimate for the total number of operation of this method f? (We consider only data movements and comparisons)

Problem 10. 10pts (4+3+3)

Function f satisfies the recurrence relation f ( n ) = 2 f ( n /2) +3 with f (1) = 1 (1) Find f (2), f (4), f (8), and f (16) (2) Find a closed form of f ( n ) when n = 2 k (3) Estimate the size of f using Big-­‐O notation if f is an increasing function.

  1. Let N be a node in a B-­‐tree. If N has m subtrees S 0 , S 1 , S 2 , …, Sm– 1 , and N’s key values are K 1 , K 2 , K 3 , …, Km– 1 , then which of the following statements is true? a) All values in subtree S 0 are greater than K 1. b) All values in subtree S 1 are less than K 1. c) All values in subtree Sm– 2 are greater than Km– 2. d) All values in subtree Sm– 1 are less than Km– 1.
  2. What type of tree is best used for indexing external data? a) B-­‐tree b) binary search tree c) 2 -­‐ 3 -­‐4 tree d) AVL tree

Problem 2. Simple Questions: 20 pts (5pts x 4)

  1. Given the following data: 25, 30, 2, 9, 8, 20. What binary search tree is created by inserting the data in the order given? T 2 for the problem 2 (2), and (3)
  2. Consider the tree T 2. In what order will an inorder traversal visit the nodes of this tree? Indicate this order by listing the labels of the nodes in the order that they are visited.

Problem 3. 10 pts (5 pts x 2)

Design an algorithm to store and restore a binary search tree T 3 into a file. After the restoration of the tree from the file, your tree does not need to keep the same shape. Your algorithm MUST improve the computational complexity of the search operation, which you can achieve with the original shape of tree T 3. Example Tree T 3 for the problem 3 (1) Describe how to store the binary search tree T 3 in a file so that you can later restore the tree.

(2) Describe how to restore the binary search tree Tyour from the file you generated in 3-­‐(1) (pseudo code, or tracing with the example tree Tyour) (3) Compare the efficiency of the search operation in your restored tree Tyour , with the one in the original tree T 3.

Problem 5. 10 pts (5 + 5)

(1) Given the following 2-­‐3 tree,T 5 -­‐(1) , draw the tree that results after inserting M, A, D, X and Z into the tree. Tree T 5 (1) : for problem 5 (1)

(2) Given the following 2-­‐3 tree, T 5 (2) , draw the tree that results after removing R, E, H and B from the tree Tree T 5 (2) : for problem 5 (2)