CS2420 Midterm II
What is the Big-O behavior of the get(int index) method in Java's ArrayList for a list of size N, when the value of index is N/2?
O(1)
What is the running-time behavior of theadd(int i, E item) method of Java'sLinkedList, where i is 0 and assuming thesize is N?
O(1)
What is the running-time behavior of theadd(int i, E item) method of Java'sArrayList, where i is 0 and assuming thesize is N? (This is equivalent to adding a newitem at arr[0] in a basic array.)
O(N)
For a balanced BST, what is the behavior of insertion in the average case? O(1) O(log N) O(N) O(N log N)
O(log N)
Assume this class definition: class Node { public char data; public Node next; public Node() { this.data = '\0'; this.next = null; } public Node(char data, Node next) { this.data = data; this.next = next; } } Which of the following single Java statements has the same effect as this sequence of Java statements: Node newNode = new Node(); newNode.data = 'n'; newNode.next = curr.next; curr.next = newNode;
curr.next = new Node('n', curr.next);
Evaluate this postfix expression: 4 5 7 2 + - * (HINT: Use a stack.)
-16
Which is a valid topological sort of the graph represented by the following DOT specification? (Select all that apply.) digraph D { 0 -> 000 00 -> 000 } 0 00 000 0 000 00 000 00 0 00 0 000 00 000 0 000 0 00
0 000 00, 00 0 000
Suppose we have quadratic-probing hash table with length 100. The hash function used is simply the identity; e.g., hash(x) = x. If an item with key 4592 is inserted and the first three locations attempted are occupied, what is the index of the next location tried? 93 96 98 99 0 1 2
1
Evaluate this postfix expression: 1 2 3 2 ^ * + (Hint: Use a stack.)
19
Consider a hash table created like so: HashTable<String, Integer> groceries = new HashTable<String, Integer>(); groceries.put("apples", 3); groceries.put("bread", 1); groceries.put("apples", 4); What should be returned by groceries.size()?
2
Suppose that the current capacity of a quadratic probing hash table is 101 (i.e., the length of the backing array is 101) and the number of entries is 52. It is time to "double" the capacity of the table. What should the new capacity be?
211
What is the minimum number of nodes in a binary tree with L leaves? L + 21.5 L + 1 2L - 1 2L 2L + 1 2(L+1)
2L - 1
Which of these algorithms solves the weighted, single-source shortest path problem? (Select all that apply.) Breadth-first search Depth-first search Dijkstra's algorithm Topological sort
Dijkstra's algorithm
Level order traversal of a binary tree uses which of the following? FIFO queue priority queue stack none of the above
FIFO queue
(T/F) Dijkstra's algorithm is guaranteed to find the shortest weighted path in a graph for which some edge weights are negative values.
False
(T/F) In a BST, if we are removing a node with two children, we can replace it with the smallest item from its left subtree and it will still be a BST.
False
(T/F) In a hash table that resolves collisions using separate chaining, rehashing is needed to ensure that all operations perform correctly when the hash table's load factor is large.
False
(T/F) The graph represented by the following DOT specification is a DAG (i.e., directed acyclic graph). digraph D { 1 -> 2 2 -> 3 3 -> 4 4 -> 5 5 -> 3 1 -> 6 6 -> 7 7 -> 5 2 -> 7 }
False
For a sparse graph, an adjacency matrix representation is an efficient use of space/memory. (T/F)
False
Which of the following snippets of Java code does not belong in a method to traverse a linked list? (Select all that apply.) Node start = temp; Node temp = start; start = start.next; temp = temp.next; temp.next = temp; while(temp != null)
Node start = temp; start = start.next; temp.next = temp;
Java's LinkedList class represents a doubly-linked list. What is the Big-O behavior of its addFirst method for a list of size N?
O(1)
What is the Big-O behavior of the add method in Java's ArrayList for a list of size N, when the value of index is N (i.e., when adding to the end of the array/list)? You should assume that, in general, the backing array does not need to grow during a call to add.
O(1)
What is the Big-O behavior of the addLast method in Java's LinkedList class for a list of size N?
O(1)
What is the running-time behavior of theget(int i) method of Java's ArrayList,assuming the size is N? (This is equivalent toarr[i] in a basic array.)
O(1)
For any BST, what is the behavior of insertion in the worst case? O(1) O(log N) O(N) O(N log N)
O(N)
Java's ArrayList class represents a basic array. As a convenience for the user, when the capacity of the backing array is exceeded, the class handles creating a new larger array and copying over the existing items. Its add(int index, E element) method inserts the specified element at the specified position. What is the Big-O behavior of this method for a list of size N, when the value of index is 0 (i.e., when adding to the beginning of the array/list)?
O(N)
Java's LinkedList is a doubly-linked list.What is the running-time behavior of theget(int i) method of Java's LinkedList,assuming the size is N?
O(N)
What is the Big-O behavior of the get(int index) method in Java's LinkedList class for a list of size N, when the value of index is N/2?
O(N)
First-In, First-Out (FIFO)
Queue
Last-In, First-Out (LIFO)
Stack
The pop operation for a stack has the same Big-O behavior as the dequeue operation for a queue. (T/F)
True
When a queue is implemented with a basic array, the Big-O behavior of the enqueue operation is the same as when it is implemented with a linked list. (T/F)
True
When a stack is implemented with a basic array, the Big-O behavior of the push operation is the same as when it is implemented with a linked list. (T/F)
True
For which data structure is finding the minimum fastest, in the average case?
balanced binary search tree
Lazy deletion is required for a hash table with which collision-resolving strategy? (Select all that apply.) linear probing quadratic probing separate chaining
linear probing quadratic probing
For a hash table with which collision-resolving strategy is it possible to have a load factor of λ = 0.4? (Select all that apply.) linear probing quadratic probing separate chaining
linear probing quadratic probing separate chaining
Each of the following hash tables contains 10 items. Which has the smallest load factor? -linear probing with initial capacity 100 and no rehashing has been performed -quadratic probing with initial capacity 11 and rehashing has been performed once -separate chaining with 5 linked lists and no rehashing has been performed
linear probing with initial capacity 100 and no rehashing has been performed
In a stack, implemented as an array or a linked list, which the following operations does not modify top? clear peek pop push
peek
The breadth-first search algorithm structure makes use of which abstract data type?
queue
For a hash table with which collision-resolving strategy can a load factor of λ = 0.7 guarantee that adding a new item is O(1)? (Select all that apply.) linear probing quadratic probing separate chaining
separate chaining
For a hash table with which collision-resolving strategy is it possible to have a load factor of λ = 2.5? (Select all that apply.) linear probing quadratic probing separate chaining
separate chaining
Which operation is more efficient in a JavaLinkedList than in a Java ArrayList? void addFirst(E element) : add at the firstposition E get(int id) : return item at position id E getFirst() : return the first item void removeLast() : remove the last element
void addFirst(E element) : add at the firstposition