Style 50 complete practice questions with full
solutions and reasoning Concordia University
50 complete practice questions with full solutions and reasoning.
Coverage: Chapter 3 asymptotic analysis, Chapter 4 lists/stacks/queues, and Chapter 7 sorting algorithms. Includes
algorithm analysis, trace questions, true/false, design comparisons, and integrated stack/queue/sorting questions.
How to use this file: Do the Question first without looking. Then read Full solution. Then read Reason / exam logic because
that is the part that teaches what to write on a midterm.
Difficulty note: Questions 21-50 are marked CHALLENGING. They are designed to force execution/tracing, not just
memorization.
Quick Reference Sheet Before Starting
Pattern Detailed idea Simplified
One constant statement T(n)=c1 Θ(1)
One loop 0 to n-1 T(n)=c1*n+c2 Θ(n)
Two full nested loops T(n)=c1*n^2+c2*n+c3 Θ(n^2)
Triangular nested loop 1+2+...+n=n(n+1)/2 Θ(n^2)
Doubling/halving loop 1,2,4,...,n or n,n/2,...,1 Θ(log n)
Geometric inner work 1+2+4+...+n < 2n Θ(n)
Mergesort T(n)=2T(n/2)+Θ(n) Θ(n log n)
Quicksort best/avg balanced partitions Θ(n log n)
Quicksort worst 0 and n-1 partitions Θ(n^2)
Practice Questions
1. Meaning of T(n), c1, c2, and Θ [BASIC]
Question: For the code below, explain what T(n), c1, c2, and Θ(n) represent. Do not only give the final answer;
explain each symbol.
int sum = 0;
for (int i = 0; i < n; i++) {
sum++;
}
Full solution: T(n) is the total amount of work done by the algorithm when the input size is n. The loop runs n times.
The repeated work inside one loop iteration is represented by c1, so the loop contributes c1*n. The fixed work
outside the loop, such as initializing sum, is represented by c2. Therefore T(n) = c1*n + c2. When simplifying to
asymptotic notation, constants are ignored, so the growth is Θ(n).
Reason / exam logic: Yan Liu usually wants you to separate the detailed cost function from the simplified growth
rate. c1 and c2 are unknown constants; you do not calculate them. They represent fixed machine-independent
,operation costs.
2. Largest element position [BASIC]
Question: For largest(A), the loop checks A[1] to A[n-1]. Give T(n), Θ, best case, and worst case.
, static int largest(int[] A) {
int currLarge = 0;
for (int i = 1; i < A.length; i++) {
if (A[currLarge] < A[i]) currLarge = i;
}
return currLarge;
}
Full solution: Let n = A.length. The loop starts at i = 1 and stops at i = n-1, so it runs n-1 times. Each iteration does
constant work: an if comparison and maybe an assignment. Thus T(n) = c1(n-1) + c2. This simplifies to Θ(n). Best
case is Θ(n), worst case is Θ(n), and average case is Θ(n).
Reason / exam logic: The position of the largest value does not stop the loop early. Even if the largest is at index 0,
the code still checks every remaining element.
3. Sequential search cases [BASIC]
Question: Analyze sequential search. Give best, worst, and average case with the condition that causes each one.
static int search(int[] A, int k) {
for (int i = 0; i < A.length; i++) {
if (A[i] == k) return i;
}
return A.length;
}
Full solution: Best case: the key is at index 0, so only one comparison is done: Θ(1). Worst case: the key is absent
or at the last index, so n comparisons are done: Θ(n). Average successful search: if the key is equally likely to be at
any position, the expected number of comparisons is (1+2+...+n)/n = (n+1)/2, so Θ(n).
Reason / exam logic: Best case does not mean n=1. Keep n fixed, then choose the easiest input of size n.
4. Single loop with final value trap [BASIC]
Question: Find T(n) and Θ. Also explain why the final value of sum is not the running time.
int sum = 0;
for (int i = 1; i <= n; i++) {
sum += n;
}
Full solution: The loop runs n times. The body is constant time, even though it adds n each time. T(n) = c1*n + c2,
so Θ(n). The final value of sum is n^2, but the algorithm did only n loop iterations.
Reason / exam logic: Complexity counts how many operations happen, not how large the computed value
becomes.
5. Nested square loop [BASIC]
Question: Give the exact summation, T(n), and Θ.
int count = 0;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
count++;
}
}
Full solution: The outer loop runs n times. For each outer iteration, the inner loop runs n times. Total count++
operations = n*n = n^2. A cost form is T(n)=c1*n^2+c2*n+c3. Therefore Θ(n^2).