Computer Science II - CSci 1200: Test 3 Preparation, Exams of Data Structures and Algorithms

An overview of computer science ii - csci 1200 test 3, including the date, time, and location. It also includes several programming questions covering topics such as mergesort, stack implementation, binary search trees, hash functions, and word ladders. Students are encouraged to study by working through sample questions and writing out solutions.

Typology: Exams

Pre 2010

Uploaded on 08/09/2009

koofers-user-3ly
koofers-user-3ly 🇺🇸

10 documents

1 / 7

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Computer Science II CSci 1200
Test 3 Overview and Practice
Overview
Test 3 will be held Friday April 18, 2008 2:00-3:45pm, Darrin 308. No make-ups will
be given except for emergency situations, and even then a written excuse from the Dean of
Students office will be required.
Coverage: Lectures 14-20, Labs 8-11, HW 7-8. Material from earlier in the semester, especially
on maps may also be covered on the test.
The test will be closed-book and closed-notes. We will provide a single sheet, attached to the
test, summarizing some of the syntax from std::map,std::list and std::set.
The test questions will be drawn from these questions, often with minor modifications, and
from the lecture, homework and lab problems. Solutions to many of these questions will be
provided a day or two before the exam.
How to study?
Work through the sample questions, writing out solutions! Redo the questions on maps
from the Test 2 practice. You are welcome (and encouraged) to do this with other
students.
Review and re-do lecture exercises, lab and homework problems.
Identify the problems that cause you difficulty and review lecture notes and background
reading on these topic areas.
Questions
1. Consider the mergesort function discussed in detail in lecture. Suppose the vector passed
to mergesort has 7 items in it. Specify the EXACT set of function calls that are made and
the exact order that they are made. In specifying this, you do not need to show the contents
of the vector, just the values of low and high (and mid, for calls to merge). This is not a
question that will appear on the test, but it is instructive about the behavior of merge sort.
2. Write an efficient function that merges two unordered vectors of integers to create a single
vector that has only the values that appear in both vectors. This single vector should have
no duplicates. As an example, if the vectors contain the values (one vector per line)
8, 11, -1, 14, 89, 27, 8, 11, 34, 17, 8, 17
-1, 12, 27, 8, 34, 17, 55, 45, 8, 17
Then the resulting vector should contain the values
-1, 8, 17, 27, 34
Much less credit will be given for a O(N2) solution, where Nis the length of the vectors,
but give such a solution if it is the only one you can think of. You may specify the function
prototype however you wish.
pf3
pf4
pf5

Partial preview of the text

Download Computer Science II - CSci 1200: Test 3 Preparation and more Exams Data Structures and Algorithms in PDF only on Docsity!

Computer Science II — CSci 1200

Test 3 — Overview and Practice

Overview

  • Test 3 will be held Friday April 18, 2008 2:00-3:45pm, Darrin 308. No make-ups will be given except for emergency situations, and even then a written excuse from the Dean of Students office will be required.
  • Coverage: Lectures 14-20, Labs 8-11, HW 7-8. Material from earlier in the semester, especially on maps may also be covered on the test.
  • The test will be closed-book and closed-notes. We will provide a single sheet, attached to the test, summarizing some of the syntax from std::map, std::list and std::set.
  • The test questions will be drawn from these questions, often with minor modifications, and from the lecture, homework and lab problems. Solutions to many of these questions will be provided a day or two before the exam.
  • How to study?
    • Work through the sample questions, writing out solutions! Redo the questions on maps from the Test 2 practice. You are welcome (and encouraged) to do this with other students.
    • Review and re-do lecture exercises, lab and homework problems.
    • Identify the problems that cause you difficulty and review lecture notes and background reading on these topic areas.

Questions

  1. Consider the mergesort function discussed in detail in lecture. Suppose the vector passed to mergesort has 7 items in it. Specify the EXACT set of function calls that are made and the exact order that they are made. In specifying this, you do not need to show the contents of the vector, just the values of low and high (and mid, for calls to merge). This is not a question that will appear on the test, but it is instructive about the behavior of merge sort.
  2. Write an efficient function that merges two unordered vectors of integers to create a single vector that has only the values that appear in both vectors. This single vector should have no duplicates. As an example, if the vectors contain the values (one vector per line)

8, 11, -1, 14, 89, 27, 8, 11, 34, 17, 8, 17 -1, 12, 27, 8, 34, 17, 55, 45, 8, 17

Then the resulting vector should contain the values

-1, 8, 17, 27, 34

Much less credit will be given for a O(N 2 ) solution, where N is the length of the vectors, but give such a solution if it is the only one you can think of. You may specify the function prototype however you wish.

  1. Consider the public stack interface.

template class stack { public: stack(); stack( stack const& other ); ~stack(); void push( T const& value ); void pop( ); T const& top( ) const; int size(); bool empty(); };

In Lab 10 you implemented the stack using a std::vector. In this question you are not allowed to use either a std::vector or a std::list. Instead you are to implement the stack using a dynamically-allocated array. To answer the question, show what private member vari- ables are needed and provide the implementation of the default constructor, the destructor, stack::push, and stack::pop. All operations should be as efficient as possible.

  1. Suppose that a monster is holding you captive on a computational desert island, and has a large file containing double precision numbers that he needs to have sorted. If you write correct code to sort his numbers he will release you and when you return home will be allowed to move on to DSA. If you don’t write correct code, he will eventually release you, but only under the condition that you retake CS 1. The stakes indeed are high, but you are quietly confident — you know about the standard library sort function. (Remember, you are supposed to have forgotten all about bubble sort.) The monster startles you by reminding you that this is a computational desert island and because of this the only data structure you have to work with is a queue. After panicking a bit (or a lot), you calm down and think about the problem. You realize that if you maintain the values in the queue in increasing order, and insert each value into the queue one at a time, then you can solve the rest of the problem easily. Therefore, you must write a function that takes a new double, stored in x, and stores it in the queue. Before the function is called, the values in the queue are in increasing order. After the function ends, the values in the queue must also be in increasing order, but the new value must also be among them. Here is the function prototype.

void insert_in_order( double x, queue& q )

You may only use the public queue interface (member functions) as specified in lab. You may use a second queue as local variable scratch space or you may try to do it in a single queue (which is a bit harder). Give an “O” estimate of the number of operations required by this function.

  1. For this question and the next few, consider the following tree node class

The characters can only be replaced one character at a time — they cannot be rearranged. Furthermore, each word must appear in the provided dictionary and words cannot be repeated in the ladder.

(a) First, write a function next word that enumerates all possible words that can be adjacent to an input word, current. The function takes a second parameter, dictionary, which is simply the set of all valid words that may appear in a word ladder. The function returns a vector of the possible next words. Here is the prototype for your function: vector next_word(const string &current, const set &dictionary); (b) (Challenging!) Now write a recursive function word ladder that uses next word and performs a brute force search to find the shortest ladder between the source and target words. For example, here is code to print the ladder between “sea” and “oil”: set dictionary; // dictionary initialization omitted vector current_ladder, shortest_ladder; current_ladder.push_back("sea"); word_ladder(current_ladder, "oil", dictionary, shortest_ladder); for (int i = 0; i < shortest_ladder.size(); i++) cout << shortest_ladder[i] << " "; cout << endl;

  1. Draw all valid three-node binary search trees containing the values 2, 5, 9.
  2. Is the following a good hash function? Why or why not?

unsigned int test3_hash( const string& key, int N ) { unsigned int value = 1; for ( int i=0; i<key.size(); i += 2 ) value = value + (key[i]*13); return value % N; }

  1. Write a function that determines if the values stored on a stack form a palindrome. Recall that a palindrome is a sequence that is the same when scanned forward and scanned backward. As examples, the following three sequences of characters are all palindromes

otto hannah able was I ere I saw elba

If any one of them was stored on a stack of chars your function should return true. The problem with using a stack is that it can not be “scanned” forward and backward (e.g. using iterators) in the sense that a vector or a list may be scanned. The only container classes your function may use are other stacks, and only the public interface of the stack class may be used:

template class stack { public: stack(); stack( stack const& other ); ~stack(); void push( T const& value ); void pop( ); T const& top( ) const; int size(); bool empty(); };

You may assume that an operator== is defined for objects of type T. To receive full credit your solution must run in time O(N ), where N is the number of items on the stack. Your function prototype must be

template bool is_palindrome( const stack& s )

  1. Suppose we implemented a hash table as a vector of vectors of keys instead of a vector of lists of keys. In other words the declaration of hash_set includes the member variables

template <class KeyType, class HashFunc> class hash_set { private: std::vector< std::list > m_table; // actual table HashFunc m_hash; // hash function unsigned int m_size; // number of keys stored in the table public:

The iterator class inside the hash_set class starts with

class iterator { public: friend class hash_set; // allows access to private variables private: hash_set* m_hs; // pointer to the hash_set object int m_index; // which entry in m_table int m_loc; // which location in m_table[m_index];

(a) The iterator class includes the following operator: iterator & operator--( ) { this->prev(); return *this; } Write the iterator class member function prev which this operator depends on. You may write it as though it is implemented inside the class declaration (and therefore you need not specify class scope in your function declaration).

public: // Return true if the value was not already there and was inserted bool insert( KeyType const& value );

// Return true if the value was there and was removed // Set status of value’s location to ERASED bool erase( KeyType const& value );

// Return true if the value is in the hash set. bool find( KeyType const& value ) const; };

Implement the insert, erase and find functions. Do not worry about the possibility of resizing the table, even though that would be important in practice.