Data Structures and Algorithms (DSA27) · Week 7

Queues (Lecture 07)

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

    "FIFO" means:

  2. W7-M02 Apply

    enqueue(1), enqueue(2), enqueue(3), dequeue(), enqueue(4), dequeue(). What does the next dequeue() return?

  3. W7-M03 Explain

    In SlowQueue, dequeue is pop(0) on a DynamicArray. Its cost on a queue of n items is:

  4. W7-M04 Explain

    An array-based queue that enqueues with insert_at(0, x) and dequeues with pop() at the end has costs:

  5. W7-M05 Apply

    A CircularQueue with capacity 8 has head = 6 and size = 3. The next enqueue writes to slot:

  6. W7-M06 Apply

    A CircularQueue with capacity 5 has head = 4 and size = 2. After one dequeue, head is:

  7. W7-M07 Recall

    A ring buffer that stores only head and tail (no size) finds head == tail. The queue is:

  8. W7-M08 Explain

    CircularQueue keeps a _size field because:

  9. W7-M09 Apply

    A full ring of capacity 4 holds [E, F, C, D] with head = 2. It grows to capacity 8 by copying new[i] = old[i] and keeping head = 2. Read front to back, the queue is now:

  10. W7-M10 Recall

    A queue on a singly linked list with head and tail references should put the front of the queue at:

  11. W7-M11 Explain

    Why not dequeue at the tail of a singly linked list?

  12. W7-M12 Apply

    A queue built from two stacks (inbox and outbox, pouring only when outbox is empty): enqueue(1), enqueue(2), enqueue(3), dequeue(). How many items are now in outbox?

  13. W7-M13 Explain

    The cost of dequeue on that two-stack queue is:

  14. W7-M14 Recall

    Which of these needs a queue?

  15. W7-M15 Apply

    n enqueues followed by n dequeues on a SlowQueue cost in total:

  16. W7-M16 Apply

    In Python, (0 - 1) % 8 is:

  17. W7-M17 Recall

    A deque is a structure that:

  18. W7-M18 Explain

    Measured, a ring-buffer dequeue and a linked-queue dequeue are both flat lines as n grows, but the ring is about five times slower. The reason:

  19. W7-M19 Apply

    Five people A, B, C, D, E stand in a circle; counting from A, every second person leaves. Who is left last?

  20. W7-M20 Apply

    An array queue with a head index but no wrap-around, 8 slots: 8 enqueues, then 5 dequeues. How many more enqueues fit without growing the block?

  21. W7-M21 Explain

    A CircularQueue that doubles its capacity when full has an enqueue cost of:

  22. W7-M22 Recall

    A priority queue (Week 12) differs from a queue because it dequeues:

  23. W7-M23 Apply

    A CircularQueue(4), starting empty: enqueue(1), enqueue(2), enqueue(3), enqueue(4), dequeue(), dequeue(), enqueue(5). Which describes the ring now?

  24. W7-M24 Apply

    A CircularQueue(5), starting empty: enqueue A, B, C, D, E; dequeue three times; enqueue F, then G. What is in the block, from slot 0 to slot 4?

  25. W7-M25 Apply

    A full CircularQueue of capacity 4 holds C D E F (front to back), with head = 2. enqueue(G) makes the correct _grow double the block first. Afterwards, head is ___ and G is in slot ___:

  26. W7-M26 Explain

    A student's _grow copies in queue order into an Array(8) and sets head = 0, but forgets to set self._capacity = 8. On the full ring of W7-M25, enqueue(G) then:

  27. W7-M27 Explain

    A dequeue updates the head with self._head += 1 instead of self._head = (self._head + 1) % self._capacity. On a CircularQueue(4): enqueue A, B, C, D; dequeue four times; enqueue E; dequeue. What happens?

  28. W7-M28 Explain

    A linked queue's enqueue tests if self._tail is None: to decide that the queue is empty, and its dequeue moves head on but never resets tail. After enqueue(1), dequeue(), enqueue(2), the next dequeue():

  29. W7-M29 Apply

    A queue from two stacks (pour only when outbox is empty). Count every push and pop on the two stacks, including the pours: enqueue 1, 2, 3; dequeue; enqueue 4, 5; dequeue three times. How many stack operations is that in total?

  30. W7-M30 Recall

    A ring buffer that leaves one slot unused to tell full from empty has a block of 8 slots. It holds at most:

  31. W7-M31 Apply

    A ring with 8 slots stores only head and tail and leaves one slot unused. Now head = 3 and tail = 2. How many items does it hold, and is it full?

  32. W7-M32 Explain

    CircularQueue.dequeue sets the slot it read back to None. Why?

  33. W7-M33 Recall

    Which of these is a typical use of a ring buffer?

  34. W7-M34 Apply

    What does this print?

    q = CircularQueue()
    q.enqueue("1")
    for _ in range(6):
        s = q.dequeue()
        print(s, end=" ")
        q.enqueue(s + "0")
        q.enqueue(s + "1")
  35. W7-M35 Apply

    Six people, 1 to 6, are put in a queue. Until one is left: move two people from the front to the back, then dequeue the third, who leaves. In what order do they leave, and who is left?

  36. W7-M36 Recall

    A graph search dequeues a node, then enqueues its unvisited neighbours, and repeats. If the queue is replaced by a stack (pop instead of dequeue, push instead of enqueue), the same loop:

  37. W7-M37 Recall

    With Python's collections.deque, the O(1) way to remove the front item of a queue d is:

  38. W7-M38 Explain

    A moving average of the last k readings keeps a CircularQueue(k) and a running total beside it. The running total is there because:

  39. W7-M39 Apply

    A CircularQueue with capacity 6 has head = 4 and size = 5. Front to back, the queue's items are in slots:

  40. W7-M40 Explain

    A naive _grow copies new[i] = old[i] and keeps head. It passes a test that fills a fresh CircularQueue(4) with A, B, C, D and then enqueues E. Why does the test not catch the bug?

32 marks

Written questions

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

  1. W7-E1 Explain 4 marks

    State the Queue ADT with the cost of each operation. Explain why an array-based queue that shifts is O(n) per dequeue, and why swapping which end is the front does not help. How does a ring buffer fix it?

  2. W7-E2 Apply 4 marks

    In a ring buffer, head == tail can mean two different things. Explain which two, with an example of each, and describe two ways of telling them apart. Which one does dsa/queue.py use?

  3. W7-E3 Explain 4 marks

    A full CircularQueue must grow. Explain, with an example, why copying the block slot by slot gives a wrong queue once the data has wrapped, give the correct copy, and explain why doubling keeps enqueue amortised O(1).

  4. W7-E4 Explain 3 marks

    Describe a queue built from two stacks. Show that it is amortised O(1) per operation, and explain why pouring must happen only when the outbox is empty.

  5. W7-E5 Explain 3 marks

    Compare the ring buffer and the linked queue (with head and tail): cost per operation, memory, and when you would choose each. Where does Python's collections.deque fit?

  6. W7-E6 Explain 4 marks

    Three ring-buffer bugs: the tail computed as head + size with no %; the head moved with head += 1 with no %; and a _grow that never updates _capacity. For each, say what goes wrong and the shortest operation sequence that shows it. Why do all three pass a test that never wraps the ring?

  7. W7-E7 Explain 3 marks

    When a ring buffer is full, it can refuse (a bounded queue) or grow. Explain both choices, give a real use for each, and say which level_order and bfs need, and why.

  8. W7-E8 Apply 4 marks

    Describe enqueue and dequeue for a queue on a singly linked list with head and tail, including the empty cases. Explain why the front must be the head, and what goes wrong if dequeue does not reset tail when the queue becomes empty.

  9. W7-E9 Explain 3 marks

    Explain why breadth-first order needs a queue, using the binary numbers 1, 10, 11, 100, … generated from "1" (dequeue s, enqueue s + "0" and s + "1"). What order do you get if the queue is replaced by a stack, and why?