CS205 Practice Exercise

0 / 18
50:00

Lists, Arrays & Complexity

18 questions • ~50 minutes • Work at your own pace

Each question has instant feedback. Use hints if stuck. Your score updates live.

When you finish, screenshot your summary and submit it on eCampus.

Section A: Linked Lists

6 questions • ~15 minutes

Q1
Trace: Insert at Head
Starting with an empty singly linked list, we perform these operations in order:
insertAtHead(3)
insertAtHead(7)
insertAtHead(1)
insertAtHead(9)
What does the list look like after all four insertions? (head → tail)
Each insertAtHead places the new node BEFORE the current head. So the last inserted element is at the front.
Q2
Code Trace: What does this print?
Node curr = head;  // list: 5 → 8 → 2 → 6
int sum = 0;
while (curr != null) {
    if (curr.data % 2 == 0) {
        sum += curr.data;
    }
    curr = curr.next;
}
System.out.println(sum);
What value is printed?
Answer:
Which values in the list are even? Add those up. Remember: 5 is odd, 8 is even, 2 is even, 6 is even.
Q3
Fix the Bug: Delete a Node
This code tries to delete the node with value target from a singly linked list. It has a bug. What's wrong?
void delete(int target) {
    Node curr = head;
    while (curr != null) {
        if (curr.data == target) {
            curr = curr.next;   // Line A
            return;
        }
        curr = curr.next;
    }
}
To remove a node, you need the PREVIOUS node to skip over it: prev.next = curr.next. Does this code do that?
Q4
Linked List Operations: Time Complexity
Match each operation to its time complexity for a singly linked list WITH both head and tail pointers.

Insert at head:

Insert at tail:

Delete at head:

Delete at tail:

With a tail pointer, you can INSERT at the tail in O(1). But to DELETE the tail, you need the node BEFORE it — and in a singly linked list, you must traverse to find it.
Q5
Predict the State
Starting with list: A → B → C → D, what's the list after these operations?
insertAfter(B, X)    // insert X after node B
deleteNode(A)        // delete the head node
insertAtHead(Z)
Trace step by step: after insertAfter(B,X) → A→B→X→C→D. After deleteNode(A) → B→X→C→D. After insertAtHead(Z) → Z→B→X→C→D.
Q6
When to Use a Linked List?
Which scenario benefits MOST from using a linked list instead of an array?
Linked lists shine when you need O(1) insert/delete at the head. Arrays require O(n) shifting for front insertions.

Section B: Arrays & ArrayLists

6 questions • ~15 minutes

Q7
Array Insert: How Many Shifts?
Given array [10, 20, 30, 40, 50, _, _] (size=5, capacity=7), how many elements must shift to insert 25 at index 2?
[10, 20, 30, 40, 50, _, _] ↑ insert 25 here (index 2) After: [10, 20, 25, 30, 40, 50, _]
Elements shifted:
All elements from the insertion point to the end must shift right by one. Count: index 2, 3, and 4.
Q8
Dynamic Array Resizing
An ArrayList starts with capacity 2 and doubles when full. After inserting 9 elements, how many times has the array been resized?
Capacity changes:
Start: capacity = 2
Insert 1, 2 → full! Resize to 4
Insert 3, 4 → full! Resize to 8
Insert 5, 6, 7, 8 → full! Resize to 16
Insert 9 → fits in capacity 16
Number of resizes:
Track the capacity: starts at 2. When full after 2 elements → 4. When full after 4 → 8. When full after 8 → 16. The 9th element fits in capacity 16.
Q9
Total Copy Cost
Using the doubling strategy, if we insert n = 16 elements into an initially empty ArrayList (starting capacity 1), what is the total number of elements copied across ALL resizes?
Capacity: 1→2→4→8→16. Resizes at capacity 1 (copy 1), at 2 (copy 2), at 4 (copy 4), at 8 (copy 8). No resize at 16 since we exactly fill it. Total copies = 1+2+4+8 = 15.
Q10
Array vs Linked List
For each operation, which is faster?
Access element at index i:
Insert at front (index 0):
Insert at end (with tail ptr / dynamic array):
Memory usage per element:
Array: O(1) random access, O(n) front insert, O(1)* end insert. Linked list: O(n) access, O(1) front insert, O(1) end insert. Arrays store just the data; linked lists need an extra pointer per node.
Q11
Array Delete: Trace the State
Given [4, 7, 2, 9, 1], what is the array after deleting the element at index 1?
Q12
Amortized Analysis: True or False
An ArrayList with doubling strategy performs addLast() n times. Which statement is TRUE?
Most addLast() calls are O(1) (just place the element). Occasionally one is O(n) due to resize. But the total copies across ALL resizes form a geometric series: 1 + 2 + 4 + ... + n ≈ 2n = O(n).

Section C: Complexity Analysis

4 questions • ~12 minutes

Q13
Rank the Growth Rates
Order these from SLOWEST growth (best) to FASTEST growth (worst):
O(n²) O(1) O(n log n) O(log n) O(n) O(2ⁿ)
Your order (slowest → fastest growth):
1st
2nd
3rd
4th
5th
6th
Think about what happens as n grows large. Constants don't grow. Logarithms grow very slowly. Linear grows steadily. n log n is slightly worse than linear. Quadratic grows fast. Exponential explodes.
Q14
Analyze This Loop
int count = 0;
for (int i = 0; i < n; i++) {
    for (int j = i; j < n; j++) {
        count++;
    }
}
What is the Big-O time complexity?
When i=0, inner loop runs n times. When i=1, it runs n-1 times. ... When i=n-1, it runs 1 time. Total = n + (n-1) + ... + 1 = ?
Q15
What's the Complexity?
int i = n;
while (i > 1) {
    i = i / 2;
}
How many times does the loop execute?
Q16
Best, Worst, and Average Case
For LINEAR SEARCH (scanning an unsorted array for a target value), match each case:

Best case:

Worst case:

Average case:

Best: target is the first element. Worst: target is last or not present. Average: on average, scan half the array — but O(n/2) = O(n) since we drop constants!

Section D: Mixed Challenges

2 questions • ~8 minutes

Q17
Design Decision
You're building a text editor's undo feature. Users type characters one at a time, and pressing Ctrl+Z undoes the last action. Which data structure is best?
Q18
Comprehensive: Code Analysis
Consider this code operating on a linked list of n elements:
Node slow = head, fast = head;
while (fast != null && fast.next != null) {
    slow = slow.next;
    fast = fast.next.next;
}
// slow is now at position ___
When the loop ends, where is slow pointing? And what is the time complexity?

slow points to:

Time complexity:

Fast moves 2 nodes per step, slow moves 1. When fast reaches the end (n steps for fast = n/2 steps total), slow has moved n/2 nodes. Where is that?

CS205 Data Structures — Exercise Results

0 / 18

Section A: Linked Lists

0/6

Section B: Arrays

0/6

Section C+D: Complexity & Mixed

0/6

Screenshot this box and submit it on eCampus!

On Mac: Cmd+Shift+4  |  On Windows: Win+Shift+S  |  On phone: screenshot button

Go to eCampus → find the assignment → attach your screenshot → submit

Review any questions marked red above for additional practice.