Contents

Note: Not all think pair share solutions are here. Some are in the notes for the corresponding day. Others may be on Canvas (in the event of a multiple choice TPS). Regardless of where the solutions are for a given TPS, the material covered is still fair game for quizzes and exams.

Jan. 29

Question 1:

Suppose I have the following class:

If I define another class as follows, will my code compile (assuming there are no errors in main)? Why or why not?

Answer: This will not compile. There are actually two bugs. First, in PolarBear, the constructor should be named PolarBear, not Bear. Second, PolarBear tries to access the private data members of Bear, which it cannot do as a subclass. We would have to make the data members 'protected' in the Bear class.

Question 2:

Ignoring what is inside the PolarBear class, suppose the first line of my PolarBear definition looks like this:

Will this code work in main? Why or why not?

Answer: This will not work. By not including 'public' before 'Bear', Bear's public methods will appear as private methods within PolarBear, so we cannot invoke myPolarBear.roar().

(Back to top)

Feb. 3

Question 1:

Is the following function recursive? Why or why not?

Answer: Yes. It makes a call back to itself (recursive case) and has a base case (if(count == 0)...).

Question 2:

Is the following function recursive? Why or why not?

Answer: No. It does not invoke itself, nor is it part of a indirect recursion (where a helper function is used in the recursion).

Question 3:

Is this function tail recursive? Why or why not?

Answer: No. The recursive call is appended to the string ".~". Since the recursive call is not by itself, it is not tail recursive.

Question 2:

What is the base case and what is the recursive case of the function above?

Answer: Lines 3 and 4: if(count == 0) return "";

(Back to top)

Feb. 24

Question 1:

Complete the following code, which swaps two elements, so that it works for arrays of any type.

Answer:

Question 2:

Say we have the following two lists of numbers:

list 1: 10, 26, 68, 74, 76, 82

list 2: 68, 26, 82, 10, 76, 74

The first list is already sorted, but if we we pass these lists to each of our sorting algorithms, they will, generally, take the same amount of time to (re)sort. It would be helpful if we could check if a list is sorted before we try to sort it. Provide an algorithm to do this. What is its complexity?

Answer: A O(n) solution is to check every adjacent pair of elements and check if the element to the right is greater than or equal to the element on the left. If that's true through the entire array, then we know the array is sorted. Here's some code:

(Back to top)

April 23

Question 1:

Name and describe two ways of dealing with collisions when inserting a key into a has table.

Answer:

Question 2:

Say I have a hash table with capacity 30 (i.e., m = 30). Now say that I am using string keys. Using the simple method of hashing we looked at last class (where we only considered the first character of a word), list the indexes the the following keys would be inserted at (don't worry about collisions):

Feel free to look up an ASCII table online to see what the decimal value of a character is.

Answer:

(Back to top)

April 30

See stack.cpp for how to use the STL stack.

(Back to top)