Midterm Review Flashcards

1
Q

Which of the JCF interfaces would be the most useful if we want to store a collection of students
enrolled in COMP2402 so that we can quickly check if a student is enrolled in COMP2402?

A

Set

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What if we also want to be able to quickly output a list of students, sorted by (lastname,firstname)?

A

SortedSet

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What if, in addition, we also want to store some auxiliary information (e.g., a mark) with each student?

A

SortedMap

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

A Bag is like a Set except that equal elements can be stored more than once. Which of the following
is best suited to implement a Bag<T>?</T>

A

b or c depending on behaviour

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

The running time of the methods get(i) and remove(i) for an ArrayList are?

A

O(1) and O(1 + size() − i), respectively

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

The running time of the methods get(i) and remove(i) for a LinkedList are?

A

O(1 + min{i, size() − i}) and O(1 + min{i, size() − i}), respectively

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

public static void insertAtBack(List<Integer> l, int n) {
for (int i = 0; i < n; i++) {
l.add(new Integer(i));
}
}
The above method is</Integer>

A

about the same speed independent of whether l is an ArrayList or a LinkedList

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

public static void frontGets(List<Integer> l, int n) {
for (int i = 0; i < n; i++) {
l.get(0);
}
}
The above method is</Integer>

A

about the same speed independent of whether l is an ArrayList or a LinkedList

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

public static void randomGets(List<Integer> l, int n) {
Random gen = new Random();
for (int i = 0; i < n; i++) {
l.get(gen.nextInt(l.size()));
}
}
The above method is</Integer>

A

much faster when l is an ArrayList

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

public static void insertAtFront(List<Integer> l, int n) {
for (int i = 0; i < n; i++) {
l.add(0, new Integer(i));
}
}
The above method is</Integer>

A

much faster when l is a LinkedList

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

public static void insertInMiddle(List<Integer> l, int n) {
for (int i = 0; i < n; i++) {
l.add(new Integer(i));
}
for (int i = 0; i < n; i++) {
l.add(n/2+i, new Integer(i));
}
}
The above method is</Integer>

A

about the same speed independent of whether l is an ArrayList or a LinkedList

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

public static void insertInMiddle2(List<Integer> l, int n) {
for (int i = 0; i < n; i++) {
l.add(new Integer(i));
}
ListIterator<Integer> li = l.listIterator(n/2);
for (int i = 0; i < n; i++) {
li.add(new Integer(i));
}
}
The above method is</Integer></Integer>

A

much faster when l is a LinkedList

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Recall that an ArrayStack stores n elements in a backing array a at locations a[0],. . . ,a[n-1]:
public class ArrayStack<T> extends AbstractList<T> {
T[] a;
int n;
...
}
Also recall that, immediately after the backing array a is resized by grow() or shrink it has a.length =
2n.
When adding an element, the ArrayStack grows the backing array a if it is full, i.e, if a.length = n.
If are currently about to grow the backing array a, what can you say about the number of add() and
remove() operations (as a function of the current value of n) since the last time the ArrayStack was
resized?</T></T>

A

At least n/2 add() operations have occurred since then

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

From the previous two questions, what can you conclude about the total number of elements copied
by grow() and shrink() if we start with an empty ArrayStack and perform m add() and remove
operations.

A

At most 2m elements are copied by grow() and shrink()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Recall that we shrink the backing array a when 3n < a.length. If we are currently about to shrink
the backing array a, what can you say about the number of add() and remove() operations since the
last time the ArrayStack was resized?

A

At least n/2 remove() operations have occurred since then

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Recall that an ArrayDeque stores n elements at locations a[j], a[(j+1)%a.length],. . . ,a[(j+n-1)%a.length]:
public class ArrayDeque<T> extends AbstractList<T> {
T[] a;
int j;
int n;
...
}
What is the amortized running time of the add(i,x) and remove(i) operations?</T></T>

A

O(1 + min{i, n − i})

11
Q

Recall that a DualArrayDeque implements the List interface using two ArrayStacks:
public class DualArrayDeque<T> extends AbstractList<T> {
ArrayStack<T> front;
ArrayStack<T> back;
...
}
In order to implement get(i) we need to get it from the ArrayStack, front or back. We can express
this as</T></T></T></T>

A

Either (b) or (c) depending on the value of i and front.size()

12
Q

If a RootishArrayStack has 10 blocks (so b.size() = 10), then how many elements can it store?

A

55

13
Q

In a RootishArrayStack, a call to get(13) will return

A

blocks.get(4)[3]

14
Q

Recall our implementation of a singly-linked list (SLList):
protected class Node {
T x;
Node next;
}
public class SLList<T> extends AbstractList<T> {
Node head;
Node tail;
int n;
...
}
Consider how to implement a Queue as an SLList. When we enqueue (add(x)) an element, where
does it go? When we dequeue (remove()) an element, where does it come from?</T></T>

A

We enqueue (add(x)) at the tail and we dequeue (remove()) at the head

15
Q

Consider how to implement a Stack as an SLList. When we push an element where does it go? When
we pop an element where does it come from?

A

We push at the head and we pop at the head

16
Q

Using the best method you can think of, how quickly can we find the ith node in an SLList?

A

in O(1 + min{i, n · (n − i − 1)}) time

17
Q

Recall our implementation of a doubly-linked list (DLList):
protected class Node {
Node next, prev;
T x;
}
public class DLList<T> extends AbstractSequentialList<T> {
protected Node dummy;
protected int n;
...
}
6
Explain the role of the dummy node. In particular, if our list is non-empty, then what are dummy.next
and dummy.prev?</T></T>

A

dummy.next is the first node in the list and dummy.prev is the last node in the list

18
Q

Consider the correctness of the following two methods that add a node u before the node p in a DLList.
protected Node add(Node u, Node p) {
u.next = p;
u.prev = p.prev;
u.next.prev = u;
u.prev.next = u;
n++;
return u;
}
protected Node add(Node u, Node p) {
u.next = p;
u.next.prev = u;
u.prev = p.prev;
u.prev.next = u;
n++;
return u;
}

A

first method

19
Q

What is the running-time of add(i,x) and remove(i) in a DLList?

A

O(1 + min{i, size() − i}) and O(1 + min{i, size() − i}), respectively