Docsity

Space Complexity and Amortized Analysis – Data Structures and Algorithms: Exam-Ready Quest, Exams of Data Structures and Algorithms

Toggle sidebar
bg1
Page 1 Data Structures and Algorithms • Exam 08
Space Complexity and Amortized Analysis – Data
Structures and Algorithms: Exam-Ready
Questions and Answers
LATEST UPDATE 2026/2027 | PREMIUM ACADEMIC EDITION
Exam 08 of 40 | Unit code: DSA-08 | Questions: 6 | Total marks: 30 | Answer ALL questions
Question 1: [4 marks]
Define space complexity. Distinguish between auxiliary space and total space.
Answer:
Space complexity measures the memory an algorithm needs as a function of input size n. Total
space includes the input itself; auxiliary space counts only the extra memory the algorithm uses
besides the input (temporary variables, recursion stack, extra arrays).
Question 2: [4 marks]
Compare the space complexity of recursive and iterative factorial.
Answer:
Recursive factorial uses one stack frame per call, so its space is O(n). Iterative factorial uses a
loop and a few variables, so its space is O(1). Both have O(n) time.
Question 3: [4 marks]
What is amortized analysis? Name its three common methods.
Answer:
Amortized analysis gives the average cost per operation over a worst-case sequence of
operations, so that a rare expensive operation is spread across many cheap ones. It does not rely
on probability. The methods are the aggregate method, the accounting (banker’s) method and
the potential method.
Question 4: [6 marks]
Use the aggregate method to show that appending to a dynamic array that doubles its capacity
is amortized O(1).
Answer:
For n appends, resizing happens when the size reaches 1, 2, 4, 8, …, and copies 1 + 2 + 4 + …
elements, which is less than 2n. The n ordinary writes cost n. The total is less than 3n, so the
amortized cost per append is below 3, i.e. O(1).
Question 5: [6 marks]
Show that incrementing a binary counter has amortized cost O(1) per increment.
pf2

Partial preview of the text

Download Space Complexity and Amortized Analysis – Data Structures and Algorithms: Exam-Ready Quest and more Exams Data Structures and Algorithms in PDF only on Docsity!

Page 1 Data Structures and Algorithms • Exam 08

Space Complexity and Amortized Analysis – Data

Structures and Algorithms: Exam-Ready

Questions and Answers

LATEST UPDATE 2026/2027 | PREMIUM ACADEMIC EDITION

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

Question 1: [4 marks]

Define space complexity. Distinguish between auxiliary space and total space.

Answer:

Space complexity measures the memory an algorithm needs as a function of input size n. Total space includes the input itself; auxiliary space counts only the extra memory the algorithm uses besides the input (temporary variables, recursion stack, extra arrays).

Question 2: [4 marks]

Compare the space complexity of recursive and iterative factorial.

Answer:

Recursive factorial uses one stack frame per call, so its space is O(n). Iterative factorial uses a loop and a few variables, so its space is O(1). Both have O(n) time.

Question 3: [4 marks]

What is amortized analysis? Name its three common methods.

Answer:

Amortized analysis gives the average cost per operation over a worst-case sequence of operations, so that a rare expensive operation is spread across many cheap ones. It does not rely on probability. The methods are the aggregate method, the accounting (banker’s) method and the potential method.

Question 4: [6 marks]

Use the aggregate method to show that appending to a dynamic array that doubles its capacity is amortized O(1).

Answer:

For n appends, resizing happens when the size reaches 1, 2, 4, 8, …, and copies 1 + 2 + 4 + … elements, which is less than 2n. The n ordinary writes cost n. The total is less than 3n, so the amortized cost per append is below 3, i.e. O(1).

Question 5: [6 marks]

Show that incrementing a binary counter has amortized cost O(1) per increment.

Page 2 Data Structures and Algorithms • Exam 08

Answer:

Bit i flips once every 2i^ increments. Over n increments the total number of flips is at most n + n/

  • n/4 + … < 2n. Dividing by n gives an amortized cost below 2 flips per increment, which is O(1), even though a single increment can flip O(log n) bits.

Question 6: [6 marks]

Give two examples of a time–space trade-off.

Answer:

  • Memoization: storing results of subproblems (extra memory) turns exponential Fibonacci into linear time.
  • Hash tables and lookup tables: using extra memory gives O(1) average lookups instead of O(n) scanning.

The reverse also occurs: recomputing values instead of storing them saves memory at the cost of time.

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