Review Questions for Prelim II

CS211 - Fall 2000

The exam will take place at 7:30pm on Tuesday, November 14.  Please arrive a bit early so that we can begin the exam right at 7:30.  We have been assigned three rooms for this exam.  Please go to the appropriate room based on the table below.  (These are exactly the same as for Prelim I.)

Upson 111 all students in the accelerated stream
Upson B17 students in regular stream whose last name begins with A through K
Philips 101 students in regular stream whose last name begins with L through Z

The use of extra material is not permitted during the exam (i.e., no textbook, no notes, no calculator, etc.).

The exam includes material covered in lecture through November 9.  Readings corresponding to the lecture material are shown on the course web pages.  The exam is primarily on topics covered since Prelim I, but it is likely that some questions will involve material from before Prelim I.

The exam will attempt to gauge your knowledge and understanding of course concepts. You should understand the terms and concepts used in the course, but you shouldn't need to memorize the specific code given in the text or in lecture.

The review questions given here are not meant to exhaust the possible topics for exam questions.  Check the online material and consider reviewing Prelim I, the lectures, and the homework assignments.

Review Questions

Problem 1

a) Write a Java class method named append that takes two non-empty linked lists L1 and L2 as parameters, and updates the last cell of L1 so that it points to the first cell of L2.  The  method does not return anything.  The lists are built using the ListCell class given below.

b) What is the asymptotic complexity of your algorithm, expressed as a function of n1 and n2, where n1 is the number of elements in L1 and n2 is the number of elements in L2, respectively?  Justify your answer (briefly).

class ListCell {
	protected Object datum;
	protected ListCell next;
	
	public ListCell (Object x, ListCell n) {
		datum = x;
		next = n;
	}
	public Object getDatum () {
		return datum;
	}
	public ListCell getNext () {
		return next;
	}
	public void setDatum (Object x) {
		datum = x;
	}
	public void setNext (ListCell l) {
		next = l;
	}
	public String toString () {
		if (next == null) return datum.toString();
		else return datum.toString + " " + next.toString();
	}
}

Problem 2

In this small GUI-based application, we have 3 lights represented by three objects of class JLabel, corresponding to the green, yellow and red lights of a traffic light signal. We have three buttons for switching on the corresponding light. Only one light can be on at a time, i.e. the lights behave like “radio buttons”. A light is switched on and off by changing the background color of the corresponding JLabel using the setBackground() method. A light is off when its color is gray (Color.gray). The colors green, yellow and red are represented by the constants Color.green, Color.yellow and Color.red respectively. Initially the green light is on. Note that these lights do not behave like true traffic lights as they are controlled by individual buttons. 

The GUI is shown in Figure (a) and the containment hierarchy is shown in Figure (b). Note that the picture is taken from an old exam (before javax.swing was released); thus, you should substitute JLabel for Label, JButton, for Button, JPanel for Panel, and JFrame for Frame.  The skeleton code is provided below. Write the method createGUI() which creates the GUI according to the containment hierarchy in Figure (b), and the method addListeners() which creates and adds the relevant listeners to the relevant sources. The user should be able to terminate the program when the “close-box” of the top-level window is clicked.

import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/**
 * Pseudo Traffic Lights
 */
public class PseudoTrafficLights extends JFrame {
	private JPanel lightsPanel;
	private JLabel redLightLabel;
	private JLabel yellowLightLabel;
	private JLabel greenLightLabel;
	private JPanel buttonsPanel;
	private JButton redLightBtn;
	private JButton yellowLightBtn;
	private JButton greenLightBtn;

// Constructor
public PseudoTrafficLights() {
	super("Traffic Lights");
	
	// Create the GUI
	createGUI();
	
	// Add the listeners to the sources
	addListeners();
	
	// Pack and show the window
	pack();		// Makes window fit tight around its contents
	setVisible(true);
}

// The main method
public static void main (String[] args) {
	new PseudoTrafficLights();
}

// Helper method to terminate the program
private void terminate() {
	dispose();
	System.exit(0);
}

// a. (13 points) Write method createGUI to set up the GUI.
private void createGUI() {



}
// b. (12 points) Write method addListeners to add listeners to the sources
private void addListeners() {



}
}

Problem 3

(a) Java Nagila, a CS211 student, implemented a bunch of algorithms and came up with the following running times as a function of input size.  Using big-O notation, write down the simples and most accurate expressions for the complexity of these algorithms.

(b) You have an algorithm that runs in time O(log n).  Java Nagila claims to have found a better algorithm that runs in time O(log(n-1)).  Is either algorithm better than the other in an asymptotic sense?  Explain your answer briefly using the formal definition of big-O notation.

(c) The following method returns the ith element of an integer list.

public static int element (List list, int i) {
	if (i == 1) return list.first();
	else return element(list.rest(), i-1);
}

What is the asymptotic time complexity of accessing the mth element of a list using this method?  Express the complexity as a function of m and n, the length of the list.

Problem 4

Fill in the table below with the expected time for each operation. Use big-O notation. The operations are insert (place a new item in the data structure), find (test if a given item is in the data structure), getMin (return the value of the minimum item in the data structure and delete it from the data structure), and successor (given an item, return the successor of that item).
Data Structure insert find getMin successor
sorted array        
unsorted array        
balanced tree        
hashtable        
sorted linked list        
unsorted linked list        

Problem 5

  1. Where is the smallest element in a Binary Search Tree?
  2. How long does it take to insert a new element into a heap? To return the smallest thing in a min-heap? To delete the smallest thing in a min-heap? To find the largest thing in a min-heap?

Problem 6

The following picture represents a BST (Binary Search Tree).

             (22)
           /      \
       (14)        (25)
      /   \       /    \
   (11)   (16) (23)     (40)
                  \     /
                 (24) (32)
  1. In what order are the nodes visited during a preorder traversal?  Inorder traversal?  Postorder traversal?
  2. Show the tree that results when 15 is inserted into the tree.
  3. Show the tree that results when 25 is deleted from the original tree.

Problem 7

For each of the following problems, choose the best of the listed data structures and explain why your choice is best. Assume that the data set is going to be large unless otherwise indicated.  Where several operations are listed you should assume, unless stated otherwise, that the operations occur with about equal frequency.
  1. Operations are Insert, DeleteMax, and DeleteMin.
    balanced tree or sorted doubly-linked list
  2. You have a dictionary containing the keywords of the Pascal programming language.
    ordered array or balanced tree
  3. You have a dictionary that can contain anywhere from 100 to 10,000 words.
    unordered linked-list or hash table
  4. You have a large set of integers with operations insert, findMax, and deleteMax.
    unordered array or Hashtable

Problem 8

You have a hash table of size m=11 and a (not very good) hash function h:

h(x) = (sum of the values of the first and last letters of x) mod m

where the value of a letter is its position in the alphabet (e.g., value(a)=1, value(b)=2, etc.). Here are some precomputed hash values:
word ape bat bird carp dog hare ibex mud koala stork
h 6 0 6 7 0 2 0 6 1 8

Draw a picture of the resulting hashtable (using chaining) after inserting, in order, the following words: ibex, hare, ape, bat, koala, mud, dog, carp, stork. Which cells are looked at when trying to find bird?  What is the load factor (lambda) for the hash table?

Problem 9

The following questions refer to an implementation of an ADT with operations Insert, Delete, and isMember. Note that these are the only operations, so for this problem it is not an advantage for a data structure to allow more operations.
  1. Under what conditions would you use a balanced tree instead of hashing with chaining?
  2. Under what conditions would you use an unordered array instead of a balanced tree?
  3. Under what conditions would you use a binary search tree instead of a heap?
  4. What implementation would you use to get the best expected time for a search?
  5. What implementation would you use to get the best worst-case time for a search?

Problem 10

BST (Dictionary) Max-Heap (Priority Queue)
      5
    /   \
   3     6
  / \
 1   4
  \
   2
     6
   /   \
  3     4
 / \
1   2

For each of the preceding trees, find a sequence of appropriate operations that will produce it starting from an empty tree.

 Problem 11

The following questions are to be answered by making use of the Java Collections Framework.

  1. Implement a double-ended queue. Operations are addFront, addBack, getFront, getBack, removeFront, removeBack, and isEmpty. The get- versions get the item without removing it. The remove- versions remove the item without retrieving it. This matches the text's definition of a double-ended queue. What is the runtime for each operation?

  2. Write a main program that prints its arguments in sorted order. What is the runtime if the number of arguments is n?

  3.  Suppose you have two maps, one mapping idNumber to name and another mapping idNumber to salary. Write code to print a table giving name and salary sorted by name. Write code to print another table giving name and salary sorted by salary. You can assume that names are unique, but you should not assume that salaries are unique. How much time does it take for each of these tables?