CS210 · Data Structures & Algorithms

Major Exam 1 — Master Question Bank

All past midterm questions · Submit to see grading & explanations

0 / 0 answered
Linked Lists
Implementation Questions
Q1
Term 161
Write a method public Node getSecondToLast(SList S) that takes a Singly Linked List as a parameter and returns a reference to the node before the last node in the list.
3 pts
Q2
Term 161
Imagine a non-empty CircularList does not have a size variable. Write the method public int size() which returns the size of this CircularList.
3 pts
Q3
Term 161
Write a method public SList cloneSList(SList S) that creates a copy of the provided Singly Linked List and returns a reference to the new list. The original list must not be destroyed.
3 pts
Q4
Term 191
Write the method void removeLast() for a singly-linked list. The method removes the last node. The list maintains size, head, and tail references.
4 pts
Q5
Term 191
Write statements to insert the node tmp after the node p in a doubly-linked list. Each node has next and prev data members.

List: A ↔ E(p) ↔ F  ·  Insert tmp(D) after p(E) → result: A ↔ E ↔ D ↔ F
3 pts
Q6
Term 191
Write a method for a circular singly-linked list that displays all data elements. Assume the size is not being maintained.
3 pts
Q7
Term 172
Write a method public Node getKthValue(Node H, int k) that takes a pointer to the Head of a Singly Linked List and an integer k. Return a pointer to the kth node. Start counting Head as position 1.
4 pts
Q8
Term 172
Given a DLL: 1 ↔ 2(G) ↔ 3 ↔ 4 ↔ 5 ↔ 6. Node G points to value 2. Write code to swap nodes containing values 2 and 5. You may create other pointers as necessary.

class Node { Node prev, next; int value; }
2 pts
Q9
Term 212
Write a method public void remove(int value) in Java within a Circular Linked List class that searches for a value and removes it from the list. Each node stores only an integer val and a next pointer.
3 pts
Q10
Term 212
Write a method to merge two sorted singly linked lists A and B into a new sorted list C.

A: 1 → 2 → 4  ·  B: 1 → 3 → 4  ·  C: 1 → 1 → 2 → 3 → 4 → 4
3 pts
Q11
Term 231
Write a Java method PrintDuplicate() extending the SinglyLinkedList class. This method prints all duplicate values in the list. Also explain why or why not your code runs in linear time.
4 pts
Q12
Term 172
Write a method createArrayOfLL that takes a Scanner to a text file containing English words (one per line). This method creates an array of 26 Singly Linked Lists, fills them with words from the file based on the word's first letter (e.g. words starting with 'a' go to A[0], 'b' to A[1], etc.), and returns a reference to this array.
3 pts
Q13
Term 252
Consider the DLL: Header ↔ [5] ↔ [10] ↔ [15] ↔ [20] ↔ Trailer, with temp pointing to node [5]. Trace this code and write/draw the sequence of values from Head to Tail after execution.
3 pts
Node A = temp.next;       // A → [10]
Node B = A.next;          // B → [15]
// Step 1
A.prev.next = B;
B.prev = A.prev;
// Step 2
A.next = B.next;
if (A.next != null) A.next.prev = A;
// Step 3
B.next = A;
A.prev = B;
Q14
Term 252
You are given an SLL L where each node stores a name (String) and next. Complete the method RemoveName(SinglyLinkedList L, String N) that searches and removes the node containing name N. Example: remove "May" from John → Ali → Zaid → May → Rose.
2 pts
public void RemoveName(SinglyLinkedList L, String N){
  if(Head.name == N){
    ____________________________________________
  }
  Node S = Head;
  Node F = S.next;
  while(F != null){
    if(F.name == N){
      ____________________________________________
    }
    _________________;
    _________________;
  }
  return;
}
MCQ
Linked Lists & Sorting
Q15
Term 231
What does the following code do?
1 pt
public Node Find(SinglyLinkedList L) {
  if (L.head == null) return null;
  Node slow = L.head;
  Node fast = L.head;
  while (fast != null && fast.next != null) {
    slow = slow.next;
    fast = fast.next.next;
  }
  return slow;
}
Q16
Term 252
Given the sequence {2, 3, 5, 6, 9, 11, 15}. Which statement is true? (Consider the pivot is the 1st index for Quick Sort).
1 pt
Q17
Term 252
To store a large matrix for Math computation in memory, which data structure is a better choice?
1 pt
Q18
Term 252
You have a Sorted Circular Linked List of size N. What is the worst-case time complexity to find and remove a specific value X?
1 pt
Q19
Term 252
The runtime needed to sort {Z, Z, Z, Z, Z, Z, Z, Z} using Merge Sort.
1 pt
Q20
Term 252
Number of swaps/exchanges sorting {0, 1, 2, 0, 1, 2, 0, 1, 2} using Selection Sort.
1 pt
Q21
Term 252
You insert N distinct integers one by one into an empty Circular Linked List, maintaining sorted order using QuickSort after every insertion. What is the Big-Oh complexity to build the full list?
1 pt
Big-O
Simplify Each Function
Q22
172 / 212 / 231
For each function f(n) below, give the asymptotic upper bound using Big-O notation.
8 pts
Function f(n) Big-O Answer
(a) f(n) = N²log N² + 2N log 2N
(b) f(n) = (N · (100N + 5 + N²))²
(c) f(n) = N1/2 + log log N
(d) f(n) = 1000 log log N + log N
(e) f(n) = 100n³ − 7n³ + 14n²
(f) f(n) = 100n³ − 100n³ + 7n²
(g) f(n) = log(7n²)
(h) f(n) = 5 log log n + 4 log²(n)
(i) f(n) = 0.001n + 100·2ⁿ
(j) f(n) = n³(1 + 6n + 2014n²)
(k) f(n) = (log n)(n + n²)
(l) f(n) = n²log n + 2n
(m) f(n) = 100n² − 100n² + 14n³
(n) f(n) = 100 log log n + 100 log²n
(o) f(n) = n³(10 + 20n + 20n²)
(p) f(n) = n²log n + 100n
Code Analysis
Find T(n) and Big-O for each snippet
Q23
Term 191
How many times is count++ executed? What is the Big-O?
2 pts
count = 0;
for(int i = 1; i < 10; i++) {
    count++;
}
Q24
Term 191
Give the runtime as T(n) and Big-O notation for the following nested loop.
3 pts
s = 0;
for (int i = 0; i < n; i++)
    for (int j = i; j < n; j++)
        s = s + 1;
Q25
Term 231
Find T(n) and Big-O for each code snippet.
5 pts
// Snippet A
int k = 0;
for (i = 1; i <= n; i++)
    for (j = 1; j <= 10; j++)
        k = k + i + j;

// Snippet B
int k = 0;
for (i = 1; i <= n; i++)
    for (j = i; j <= n; j++)
        k = k + i;

// Snippet C
for (i = n; i >= 1; i /= 2)
    System.out.println(i);
Q26
Term 212
Estimate T(n) and Big-O for the following code.
2 pts
void f1(int n) {
    for(int i = 0; i < n; i++) {
        for(int j = 0; j < 10; j++) {
            for(int k = 0; k < n; k++) {
                for(int m = 0; m < 10; m++) {
                    System.out.println("!");
                }
            }
        }
    }
}
Q27
Term 252
Find T(n) and Big-O for the two code snippets below.
2 pts
// Snippet 1 — processFixedGrid
public void processFixedGrid(int n) {
    int sum = 0;
    for (int i = 1; i <= 10; i++)
        for (int j = 1; j <= 5; j++)
            sum = sum + i + j;
    System.out.println("Result " + n + " is " + sum);
}

// Snippet 2 — sqrtSearch (recursive, trace for sqrtSearch(n, 0, n))
public int sqrtSearch(int n, int low, int high) {
    if (low > high) return high;
    int mid = (low + high) / 2;
    long square = (long) mid * mid;
    if (square == n) return mid;
    if (square < n) return sqrtSearch(n, mid + 1, high);
    else return sqrtSearch(n, low, mid - 1);
}
Recursion
Trace & Complexity
Q28
Term 172
Estimate the number of operations and worst-case Big-O for silly(8,0). Show the trace.
2 pts
public int silly(int n, int m) {
    if (n < 1) return m;
    else return silly(n/2, m);
}
// Call: silly(8, 0)
Q29
Term 191
Show the trace for the following recursive method. Estimate Big-O. Use top-level call: recur(32)
3.5 pts
int recur(int n) {
    if (n == 1) return 0;
    return 1 + recur(n / 2);
}
// Call: recur(32)
Q30
Term 212
Trace f2(15, 5) and give the Big-O notation.
4 pts
int f2(int n, int m) {
    if (n == 0) return 1;
    if (n % 2 == 0) return f2(n/2, m);
    else return f2((n-1)/2, m-1);
}
// Trace: f2(15, 5)
Q31
Term 252
Trace productDigits(2121) — fill in the table. What is the Big-O? Then complete the linked-list version productDigitsList(Node head).
7 pts
public static int productDigits(int n) {
    if (n == 0) return 1;
    return (n % 10) * productDigits(n / 10);
}
// Example: productDigits(234) = 2 × 3 × 4 = 24
Q32
Term 231
Give the running time T(n) and Big-O for fun(n). Show the output when n=3 and draw the recursion trace.
2 pts
static void fun(int n) {
    if (n < 1) return;
    else {
        System.out.println(n);
        fun(n - 1);
        System.out.println(n);
        return;
    }
}
// Call: fun(3)
Algorithm Comparison
When is A better than B?
Q33
Term 191
Which algorithm has better time complexity?

(i) Algorithm A: step count 2100 + log n100
(ii) Algorithm B: step count n + 2 log n
1 pt
Q34
Term 212 / 231
Algorithm A takes TA(n) = n³ + 5n² + 100n. Algorithm B takes TB(n) = 1000n² + 1000n. When is B more efficient than A? Give the crossover values of n₀ and constant c.
1 pt
Q35
Term 231
Algorithms A and B spend exactly TA(n) = 5·n·log₂n and TB(n) = 25·n microseconds respectively. Which algorithm is better in terms of time complexity? For which problem size (c and n₀) does it outperform the other?
1 pt
Sorting
Trace & Analysis
Q36
Term 252
Show the trace of Insertion Sort to sort the following array. Show all individual steps and the values of i and j at the end of each swap.

[ K | R | A | T | E | L | P | U | I ]
Alphabet order: A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
3 pts
Q37
Term 161
Write a method using iteration to compute and return the sum of all positive integers in a 1D array. Analyse the algorithm showing number of operations and give the Big-O for best and worst cases.

Array A = [3, -1, -6, 0, -8, 4, 2, 1, -1, -2, 1]
3 pts
Q38
Term 161
Write the same method from Q37 using recursion. Show recurrences using tracing and estimate the Big-O using the substitution method.
3 pts
Q39
Term 191
Write a recursive method to sum the odd-integer values in an array. Give best and worst-case Big-O.

Example: [1, 2, 3, 4, 5, 6, 7] → sum of odds = 16
3.5 pts
Your Score
–%
– / – questions
0
Correct
0
Incorrect
0
Partial

Scroll up to see detailed feedback under each question.