Docsity

Algorithm Analysis and Big-O Notation – Data Structures and Algorithms: Exam-Ready Questio, Exams of Data Structures and Algorithms

Toggle sidebar
bg1
Page 1 Data Structures and Algorithms • Exam 07
Algorithm Analysis and Big-O Notation – Data
Structures and Algorithms: Exam-Ready
Questions and Answers
LATEST UPDATE 2026/2027 | PREMIUM ACADEMIC EDITION
Exam 07 of 40 | Unit code: DSA-07 | Questions: 6 | Total marks: 30 | Answer ALL questions
Question 1: [5 marks]
Define Big-O, Big-Omega and Big-Theta notation.
Answer:
• f(n) = O(g(n)) if there are constants c > 0 and n0 such that f(n) ≤ c·g(n) for all n ≥ n0 (an upper
bound).
• f(n) = Ω(g(n)) if f(n) ≥ c·g(n) for all n ≥ n0 (a lower bound).
• f(n) = Θ(g(n)) if f(n) is both O(g(n)) and Ω(g(n)) (a tight bound).
Question 2: [5 marks]
Determine the time complexity of: for i in 1..n: for j in 1..i: print(i, j)
Answer:
The inner loop runs i times for each i, so the total is 1 + 2 + … + n = n(n + 1)/2, which is O(n2).
Question 3: [4 marks]
Arrange in increasing order of growth: n!, n2, log n, 2n, n log n, n, 1.
Answer:
1 < log n < n < n log n < n2 < 2n < n!
Question 4: [5 marks]
What is the complexity of: i = 1; while i < n: i = i * 2 ?
Answer:
The value of i doubles each time (1, 2, 4, 8, …), so the loop ends after about log2 n iterations.
Complexity: O(log n).
Question 5: [5 marks]
Using linear search as an example, explain best-case, worst-case and average-case analysis.
Answer:
• Best case: the target is the first element, 1 comparison, O(1).
• Worst case: the target is last or absent, n comparisons, O(n).
• Average case: if the target is equally likely at any position, about (n + 1)/2 comparisons, which
is O(n).
pf2

Partial preview of the text

Download Algorithm Analysis and Big-O Notation – Data Structures and Algorithms: Exam-Ready Questio and more Exams Data Structures and Algorithms in PDF only on Docsity!

Page 1 Data Structures and Algorithms • Exam 07

Algorithm Analysis and Big-O Notation – Data

Structures and Algorithms: Exam-Ready

Questions and Answers

LATEST UPDATE 2026/2027 | PREMIUM ACADEMIC EDITION

Exam 07 of 40 | Unit code: DSA-07 | Questions: 6 | Total marks: 30 | Answer ALL questions

Question 1: [5 marks]

Define Big-O, Big-Omega and Big-Theta notation.

Answer:

  • f(n) = O(g(n)) if there are constants c > 0 and n0 such that f(n) ≤ c·g(n) for all n ≥ n0 (an upper bound).
  • f(n) = Ω(g(n)) if f(n) ≥ c·g(n) for all n ≥ n0 (a lower bound).
  • f(n) = Θ(g(n)) if f(n) is both O(g(n)) and Ω(g(n)) (a tight bound).

Question 2: [5 marks]

Determine the time complexity of: for i in 1..n: for j in 1..i: print(i, j)

Answer:

The inner loop runs i times for each i, so the total is 1 + 2 + … + n = n(n + 1)/2, which is O(n^2 ).

Question 3: [4 marks]

Arrange in increasing order of growth: n!, n^2 , log n, 2n, n log n, n, 1.

Answer:

1 < log n < n < n log n < n^2 < 2n^ < n!

Question 4: [5 marks]

What is the complexity of: i = 1; while i < n: i = i * 2?

Answer:

The value of i doubles each time (1, 2, 4, 8, …), so the loop ends after about log2 n iterations. Complexity: O(log n).

Question 5: [5 marks]

Using linear search as an example, explain best-case, worst-case and average-case analysis.

Answer:

  • Best case: the target is the first element, 1 comparison, O(1).
  • Worst case: the target is last or absent, n comparisons, O(n).
  • Average case: if the target is equally likely at any position, about (n + 1)/2 comparisons, which is O(n).

Page 2 Data Structures and Algorithms • Exam 07

Question 6: [6 marks]

Solve T(n) = 2T(n/2) + n using the Master theorem.

Answer:

Here a = 2, b = 2 and f(n) = n. Compute nlogb^ a^ = nlog^2 2 = n. Since f(n) = Θ(nlogb^ a), case 2 applies and T(n) = Θ(n log n).

Study answers prepared for revision purposes. Always cross-check against your lecturer’s notes and prescribed textbook.