Fall 2025 Final Exam Practice Key
CSCI 1913 – Introduction to Algorithms, Data Structures, and Program Development
1 Array-based lists
- Draw an array of size 5, which has the following values appended in the following order: 32, 15, 6, 50
| 32 | 15 | 6 | 50 |
- Draw the array from part (a) with value 15 deleted
| 32 | 6 | 50 |
- Draw the array from part (b) with the value 44 inserted at index 1
| 32 | 44 | 6 | 50 |
2 Linked-list
- Draw an empty linked list with a
headand atailreferences
- Draw the linked list from part (a) with the following values added in this order: 32, 15, 6, 50
- Draw the linked list from part (b) with the node of value 15 deleted
3 Binary Search Tree
- Draw a binary search tree which has the following values inserted in the following order: 32, 15, 6, 50, 25, 21, 43, 73, 36, 37, 68, 23, 75, 34
- Draw the binary search tree from part (a) with value 25 deleted, and the value 44 inserted
- Draw the binary search tree from part (b) with value 43 deleted, and the value 29 inserted
4 Hash Table
Consider an initially empty hash table \(H\) of size 6, designed to store integer keys with linear probing. The hash function is defined as:
\(hash(k) = k \mod length(H)\)
When the load factor (λ) exceeds 0.7, the table size is doubled.
Insert the following sequence of keys in the given order: 12, 18, 21, 2, 26, 17, 34 and show the final hash table configuration.
| 12 | 2 | 26 | 17 | 18 | 21 | 34 |
5 Big-O
Consider the following python code.
def loreum(n):
lst1 = []
for i in range(n):
j = 0
while j < n:
lst1.append(i)
lst1.append(j)
j *= 2
set2 = set() # A python set, add is O(1)
for val in lst1:
set2.add(val)
return set2Identify the Big O notation for this function:
▢ a) \(O(1)\)
▢ b) \(O(\log n)\)
▢ c) \(O(n)\)
✅ d) \(O(n \log n)\)
▢ e) \(O(n^2)\)
▢ f) \(O(n^3)\)
▢ g) \(O(n^4)\)
▢ h) \(O(2^n)\)
Write a function which given the same argument as
loreumwill result in the same return result but has a better runtime. (You may use either python or java to solve this problem, the python set issetthe java set you can assume isSetwith a generic).
A possible answer (remove the nested loops, make them happen one after the other, adding to set instead of a list first) – \(O(n)\)
def loreum(n):
set2 = set()
for i in range(n):
set2.add(i)
j = 1
while j < n:
set2.add(j)
j *= 2
return valIt can be simplified as so because the while loop is adding repeated elements anyway:
def loreum(n):
set2 = set()
for i in range(n):
set2.add(i)
return set2Simpler yet:
def loreum(n):
return set(range(n))6 Sorting
- Given the array [8, 3, 5, 11, 2, 9], show what the array would be after each pass of insertion sort. (insertion sort has two nested loops, a pass in this case is one iteration of the outer loop).
[3, 8, 5, 11, 2, 9]
[3, 5, 8, 11, 2, 9]
[3, 5, 8, 11, 2, 9]
[2, 3, 5, 8, 11, 9]
[2, 3, 5, 8, 9, 11]
- Given the array [8, 3, 5, 11, 2, 9], show what the array would be after each pass of bubble sort. (bubble sort has two nested loops, a pass in this case is one iteration of the outer loop).
[3, 5, 8, 2, 9, 11]
[3, 5, 2, 8, 9, 11]
[3, 2, 5, 8, 9, 11]
[3, 2, 5, 8, 9, 11]
Bubble sort always have a repeated last pass to make sure everything is sorted.
- Given the array [8, 3, 5, 11, 2, 9], show what the array would be after each pass of selection sort. (selection sort has two nested loops, a pass in this case is one iteration of the outer loop).
[2, 3, 5, 11, 8, 9]
[2, 3, 5, 11, 8, 9]
[2, 3, 5, 11, 8, 9]
[2, 3, 5, 8, 11, 9]
[2, 3, 5, 8, 9, 11]
7 Java Writing
- Write a java class named
Shelf. The class must implement theComparableinterface to allow comparison betweenShelfobjects based on the items they contain. Follow these requirements:
The class should have two instance variables which are generics of the same type that are comparable. The Shelf class should also be comparable.
- a constructor that accepts two arguments to be assigned to instance variables
oneandtwo. - a
compareTomethod which returns thecompareToof the argument with either the value inoneandtwo. If argument is not equal toonethan it returns the comparison of argument andone, otherwise it returns the comparison of argument and two.
public class Shelf<T extends Comparable> implements Comparable<Node<T>>{
private T one;
private T two;
public Shelf(T one, T two) {
this.one = one;
this.two = two;
}
public int compareTo(T argument) {
if (!argument.equals(one)) return one.compareTo(argument);
return two.compareTo(argument);
}
}
- The
LinkedSetclass represents a set of distinct elements (no duplicates) implemented using a linked data structure (the code forNode.javacan be found below). These elements are stored as the data values of a linked list, arranged in increasing order. Implement thesize,add, andremovemethods.
public LinkedSet<T extends Comparable<T>>{
private Node<T> head;
public int size() {
int size = 0;
Node<T> current = head;
while (current != null) {
size++;
current = current.getNext();
}
return size;
}
public void add(T obj) {
if (head == null) head = new Node(obj);
else {
Node<T> current = head;
Node<T> previous = null;
while (current != null && current.compareTo(obj) != 0) {
previous = current;
current = current.getNext();
}
if (current == null) previous.setNext(new Node(obj));
}
}
public void remove(T obj) {
if (head.getData().compareTo(obj) == 0) head = head.getNext();
else {
Node<T> current = head;
Node<T> previous = null;
while (current != null && current.compareTo(obj) != 0) {
previous = current;
current = current.getNext();
}
if (current != null) previous.setNext(current.getNext());
}
}