Data Structures and Algorithms (DSA27) · Week 5

Linked lists (Lecture 05)

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. W5-M01 Recall

    A node of a singly linked list holds:

  2. W5-M02 Recall

    Reading ll[i] on a linked list of n nodes costs:

  3. W5-M03 Recall

    push_front costs:

  4. W5-M04 Recall

    append on a singly linked list without a tail reference costs:

  5. W5-M05 Explain

    Which operation is O(1) on a linked list but O(n) on a dynamic array?

  6. W5-M06 Explain

    Which operation is O(1) on a dynamic array but O(n) on a linked list?

  7. W5-M07 Explain

    In push_front, why must new.next = self.head come before self.head = new?

  8. W5-M08 Explain

    To remove a node from a singly linked list, you need a reference to:

  9. W5-M09 Recall

    Which node is the special case in remove(value) without a sentinel?

  10. W5-M10 Apply

    What is the running time of this loop over a linked list of n nodes?

    for i in range(len(ll)):
        total += ll[i]
  11. W5-M11 Explain

    Why is len(ll) O(1) in dsa/linked_list.py?

  12. W5-M12 Apply

    Reversing a linked list in place needs how much extra space?

  13. W5-M13 Recall

    On 64-bit CPython, a Node with __slots__ takes about 48 bytes. One slot of an array of references takes:

  14. W5-M14 Explain

    A doubly linked list can do in O(1) what a singly linked one cannot:

  15. W5-M15 Explain

    What is a sentinel (dummy) node for?

  16. W5-M16 Explain

    A singly linked list with a tail reference. Removing the last node costs:

  17. W5-M17 Recall

    Python's collections.deque is implemented as:

  18. W5-M18 Explain

    Why does the traversal loop test while node is not None: rather than while node.next is not None:?

  19. W5-M19 Apply

    After a = Node(1), b = Node(2, a), c = Node(3, b), what is c.next.next.value?

  20. W5-M20 Recall

    In Python, what happens to a node that nothing refers to any more?

  21. W5-M21 Apply

    To insert a new node at index i ≥ 1, you walk to the node at index:

  22. W5-M22 Recall

    Where in this course do short linked lists appear inside another structure?

  23. W5-M23 Explain

    What is printed?

    ll = LinkedList([1, 2, 3, 4])
    node = ll.head.next
    node.next = node.next.next
    print(list(ll), len(ll))
  24. W5-M24 Apply

    On a list of 6 nodes, insert_at(4, x) runs. How many times does prev = prev.next run?

  25. W5-M25 Recall

    ll = LinkedList([1, 2, 3]). Which call raises IndexError?

  26. W5-M26 Recall

    In a list with a sentinel node, the list is empty when:

  27. W5-M27 Apply

    In a doubly linked list, x is a node that is neither the first nor the last. Which two assignments remove it from the list?

  28. W5-M28 Apply

    A new node is inserted between two neighbouring nodes a and b of a doubly linked list. How many references must be set, counting the new node's own?

  29. W5-M29 Explain

    A student adds _tail to the list. Their append decides "empty" by testing self._tail is None, and their pop_front updates head and _size but never _tail. What does this print?

    ll = LinkedList([7])
    ll.pop_front()
    ll.append(5)
    print(list(ll), len(ll))
  30. W5-M30 Apply

    reverse() runs on 1 → 2 → 3. After the first pass of the loop body, what is the state?

  31. W5-M31 Explain

    Suppose insert_at had no index == 0 case and went straight to the walk. What would insert_at(0, 9) do to [1, 2, 3]?

  32. W5-M32 Recall

    ll = LinkedList([4, 2, 4]), then ll.remove(4). Now list(ll) is:

  33. W5-M33 Explain

    Walking a dynamic array and walking a linked list are both O(n), yet the array walk is usually several times faster. Why?

  34. W5-M34 Explain

    In a circular list the last node points back to the first. What does the usual loop while node is not None: do on it?

  35. W5-M35 Apply

    What is printed?

    a, b, c = Node(1), Node(2), Node(3)
    a.next = b
    b.next = c
    b.next, c.next = None, b
    print(a.next.value, c.next.value, b.next)
  36. W5-M36 Recall

    LinkedList().pop_front():

  37. W5-M37 Explain

    You hold a reference node to a node in the middle of a long singly linked list. Inserting a new node immediately before it costs:

  38. W5-M38 Apply

    What is printed?

    ll = LinkedList()
    for v in [3, 1, 2]:
        ll.push_front(v)
    ll.append(ll.pop_front())
    print(list(ll))
  39. W5-M39 Recall

    insert_at(2, x) on a singly linked list of 5 nodes. How many references held by nodes that were already in the list change?

  40. W5-M40 Explain

    A singly linked list with head and _tail is reversed in place. What must happen to _tail?

33 marks

Written questions

9 written questions (33 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 / 33

  1. W5-E1 Explain 4 marks

    Compare a dynamic array and a singly linked list on four operations, with their costs. For each structure, give one situation where you would choose it, and say why.

  2. W5-E2 Apply 4 marks

    Explain push_front step by step with a diagram. Show what goes wrong if the two assignments are done in the other order.

  3. W5-E3 Explain 3 marks

    LinkedList supports ll[i], just like an array. Explain why the cost is different, and why for i in range(len(ll)): ll[i] is a bad loop. What should be written instead?

  4. W5-E4 Explain 4 marks

    A tail reference makes append O(1). What does it cost? Name the methods that must change, and explain why removing the last node is still O(n).

  5. W5-E5 Explain 3 marks

    Explain what a doubly linked list and a sentinel node each fix, and what each costs.

  6. W5-E6 Explain 4 marks

    A music player keeps a playlist. The user presses next and previous, and can delete the song that is playing. Which structure would you choose: a dynamic array, a singly linked list or a doubly linked list? Give the cost of each of the three actions in your choice, and say why each of the other two is worse.

  7. W5-E7 Apply 4 marks

    Explain insert_at(index, value) step by step: the check on index, the case index == 0, the walk, and the splice. Why does the walk stop after index - 1 steps? Give the best-case and worst-case cost, and show what goes wrong if the loop is written for _ in range(index):, both for an index in the middle and for index == len(ll).

  8. W5-E8 Explain 3 marks

    A linked list and a dynamic array are both O(n) to walk, yet measurement shows the array is faster and smaller. Explain both differences, with numbers for CPython. When is the linked list still the right choice?

  9. W5-E9 Explain 4 marks

    Explain how reverse() reverses a list in place with three references. State what prev and current mean at the start of each pass of the loop, why following must be saved first, and why the extra space is O(1). Compare with building a reversed copy with push_front.