

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


Page 1 Data Structures and Algorithms • Exam 08
LATEST UPDATE 2026/2027 | PREMIUM ACADEMIC EDITION
Exam 08 of 40 | Unit code: DSA-08 | Questions: 6 | Total marks: 30 | Answer ALL questions
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).
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.
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.
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).
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/
Give two examples of a time–space trade-off.
Answer:
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.