sample_midterm_with_solutions

# sample_midterm_with_solutions - CSE 2100 Sample Mid-Term...

This preview shows pages 1–2. Sign up to view the full content.

1 CSE 2100 Sample Mid-Term Exam with Solutions (5 Points) Algorithm A takes n 2 days to solve a problem of size n. Algorithm B takes n 3 seconds on the same problem. How large a problem instance do you need before algorithm A becomes faster than algorithm B? How much time do the algorithms take on that instance? Solution: Since there are 24*60*60=86,400 seconds in a day, for the instance size n for which the two algorithms take the same amount of time we have 86,400* n 2 = n 3 . Thus, n= 86,400. For this input size the two algorithms take 86,400 2 days, or about 20.4 million years. (5 Points) Prove or disprove: if f(n) and g(n) are both O(h(n)), then f(n) + g(n) is O(h(n)). Solution: The claim is true. Indeed, assume that f(n) and g(n) are both O( h(n) ). Then, by definition, there exist constants c f , c g > 0 and integers n f , n g 1 such that f(n) c f h(n) for every n n f and g(n) c g h(n) for every n n g . Thus, for every n max{ n g , n f }, f(n)+g(n) c f h(n)+c g h(n)= ( c f +c g ) h(n) , which shows that f(n) + g(n) is O(h(n)) . (10 Points) Consider a restricted sequence ADT that provides only the following methods for a sequence S: insertFirst (e) inserts element e into the first position of S and returns its position; remove (p) removes from S the element at position p and returns it;

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 03/27/2011 for the course CSE 2100 taught by Professor Staff during the Spring '08 term at UConn.

### Page1 / 3

sample_midterm_with_solutions - CSE 2100 Sample Mid-Term...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online