Data Structures and Algorithms (DSA27) · Week 4

Dynamic arrays and amortised analysis (Lecture 04)

These quizzes are for self-study. Your answers are saved on this device only and are not sent anywhere.

Score: 0 / 40 · Answered: 0

  1. W4-M01 Recall

    For a DynamicArray, len(a) returns:

  2. W4-M02 Apply

    With doubling, what is the capacity after 5 appends?

  3. W4-M03 Apply

    With doubling, how many resizes do 1,000 appends cause?

  4. W4-M04 Apply

    If the capacity grows by one slot at every resize, the total number of copies for n appends is:

  5. W4-M05 Explain

    If the capacity grows by a constant 100 slots at every resize, the amortised cost of append is:

  6. W4-M06 Recall

    With doubling, the amortised cost of append is:

  7. W4-M07 Explain

    With doubling, the worst-case cost of a single append is:

  8. W4-M08 Explain

    How does amortised cost differ from average-case cost?

  9. W4-M09 Apply

    With doubling, the total number of element copies for n appends is:

  10. W4-M10 Apply

    With doubling, which of these appends triggers a resize?

  11. W4-M11 Explain

    To shrink without thrashing, a dynamic array should halve its capacity when the size falls to:

  12. W4-M12 Recall

    "Thrashing" in a dynamic array means:

  13. W4-M13 Recall

    CPython's list grows its capacity by about:

  14. W4-M14 Apply

    How can you see a Python list's capacity from Python?

  15. W4-M15 Apply

    In a DynamicArray of size 5 and capacity 8, a[-1] reads self._block at index:

  16. W4-M16 Recall

    insert_at(0, x) on a dynamic array of size n costs:

  17. W4-M17 Recall

    pop() — removing the last element — costs:

  18. W4-M18 Explain

    Why does growing by a factor give amortised O(1) append?

  19. W4-M19 Explain

    Growing by a factor of 1.5 instead of 2:

  20. W4-M20 Apply

    With doubling and no shrinking, just after a resize, about how much of the capacity is unused?

  21. W4-M21 Recall

    Which structure in dsa/ stores its items in the student's own DynamicArray?

  22. W4-M22 Apply

    In the accounting ("bank") proof for doubling, how many coins must each append be charged?

  23. W4-M23 Apply

    With the course DynamicArray (capacity 1, doubling), what are len(a) and a._capacity after a = DynamicArray(range(6))?

  24. W4-M24 Explain

    Why does __getitem__ check index < self._size rather than index < self._capacity?

  25. W4-M25 Apply

    What does this print?

    a = DynamicArray()
    for i in range(9):
        a.append(i)
    print(a.resize_count, a._capacity)
  26. W4-M26 Recall

    When the course DynamicArray resizes, it:

  27. W4-M27 Explain

    Why can _resize not simply make the existing Array longer?

  28. W4-M28 Apply

    A DynamicArray holds 8 elements and has capacity 8. Counting the elements copied by a resize and the elements shifted, how many existing elements move during insert_at(0, "x")?

  29. W4-M29 Apply

    a = DynamicArray([10, 20, 30, 40, 50]). What does a.pop(2) return, and how many elements move?

  30. W4-M30 Apply

    After 9 appends and then 8 calls of pop(), what is the capacity of the course DynamicArray?

  31. W4-M31 Apply

    During 1,000,000 appends to an array that doubles, the single most expensive append copies how many elements?

  32. W4-M32 Apply

    With DynamicArray(growth=3), the total number of elements copied by n appends is always less than:

  33. W4-M33 Recall

    Which of these statements is an amortised bound rather than a worst-case bound for a single operation?

  34. W4-M34 Recall

    DynamicArray(growth=1.5) fails at its first resize. Why?

  35. W4-M35 Explain

    What does DynamicArray([1, 2], growth=1) do?

  36. W4-M36 Explain

    An array that grows by a constant +10 slots copies 798,400 elements for 4,000 appends. For 8,000 appends it copies about:

  37. W4-M37 Recall

    A DynamicArray holds 5 elements in capacity 8. What does a[5] = "x" do?

  38. W4-M38 Explain

    In the accounting argument (3 coins per append), the doubling from capacity 8 to 16 copies 8 elements. Which coins pay for it?

  39. W4-M39 Explain

    A student's _resize copies into the new block and sets self._block, but forgets self._capacity = capacity. Starting empty with capacity 1, what happens?

  40. W4-M40 Recall

    Which methods of the course DynamicArray can trigger a resize?

34 marks

Written questions

9 written questions (34 marks). Write your answer first, then open the model answer and its marking points, and mark yourself honestly: each point is worth about one mark.

Written total: 0 / 34

  1. W4-E1 Explain 5 marks

    Define amortised cost. Prove that n appends to a dynamic array that doubles cost O(n) in total, and conclude the amortised cost of one append.

  2. W4-E2 Explain 4 marks

    Explain why growing the capacity by a constant number of slots gives Θ(n2) total cost for n appends, however large the constant, while growing by a constant factor gives Θ(n).

  3. W4-E3 Explain 3 marks

    append is "worst-case O(n) and amortised O(1)". Explain both statements, why both are true, and when the worst case still matters.

  4. W4-E4 Explain 4 marks

    Explain thrashing with a concrete sequence of operations, and the shrinking rule that prevents it.

  5. W4-E5 Explain 3 marks

    Growth factor is a time–space trade-off. Explain the trade-off, and why CPython's list uses a small factor (about 1.125).

  6. W4-E6 Apply 4 marks

    A DynamicArray holds a b c d in capacity 4. Describe every step of insert_at(1, "x"), give the final contents, size and capacity, and count the elements copied and shifted. Why must insert_at check for a full array before it shifts?

  7. W4-E7 Explain 4 marks

    Stack, SlowQueue, MinHeap and Graph are all built on your DynamicArray. For the stack and the slow queue, explain how the cost of each operation follows from the DynamicArray table. Why does a bug in DynamicArray show up in the tests of other weeks?

  8. W4-E8 Explain 3 marks

    Explain the accounting ("bank") argument for a doubling array: how many coins each append is charged, what each coin pays for, and why the bank never goes negative.

  9. W4-E9 Explain 4 marks

    A game appends one record to a dynamic array every frame and must never take longer than one frame. Explain why a doubling array is a risk even though append is amortised O(1), and describe two ways to remove the risk.