هياكل البيانات والخوارزميات (DSA27) · الأسبوع 7

Queues (Lecture 07)

هذه اختبارات للتدريب الذاتي، تُحفظ إجاباتك على هذا الجهاز فقط ولا تُرسل إلى أحد. الأسئلة والشرح بالإنجليزية لغة المقرر.

النتيجة: 0 / 40 · أُجيب: 0

  1. W7-M01 تذكّر

    "FIFO" means:

  2. W7-M02 تطبيق

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

  3. W7-M03 تعليل

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

  4. W7-M04 تعليل

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

  5. W7-M05 تطبيق

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

  6. W7-M06 تطبيق

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

  7. W7-M07 تذكّر

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

  8. W7-M08 تعليل

    CircularQueue keeps a _size field because:

  9. W7-M09 تطبيق

    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 تذكّر

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

  11. W7-M11 تعليل

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

  12. W7-M12 تطبيق

    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 تعليل

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

  14. W7-M14 تذكّر

    Which of these needs a queue?

  15. W7-M15 تطبيق

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

  16. W7-M16 تطبيق

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

  17. W7-M17 تذكّر

    A deque is a structure that:

  18. W7-M18 تعليل

    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 تطبيق

    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 تطبيق

    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 تعليل

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

  22. W7-M22 تذكّر

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

  23. W7-M23 تطبيق

    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 تطبيق

    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 تطبيق

    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 تعليل

    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 تعليل

    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 تعليل

    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 تطبيق

    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 تذكّر

    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 تطبيق

    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 تعليل

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

  33. W7-M33 تذكّر

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

  34. W7-M34 تطبيق

    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 تطبيق

    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 تذكّر

    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 تذكّر

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

  38. W7-M38 تعليل

    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 تطبيق

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

  40. W7-M40 تعليل

    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 درجات

أسئلة المقال

9 أسئلة مقالية (32 درجة). اكتب إجابتك أولًا، ثم اعرض الإجابة النموذجية ونقاط التصحيح، وقدّر درجتك بأمانة: كل نقطة تساوي درجة تقريبًا.

مجموع درجات المقال: 0 / 32

  1. W7-E1 تعليل 4 درجات

    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 تطبيق 4 درجات

    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 تعليل 4 درجات

    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 تعليل 3 درجات

    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 تعليل 3 درجات

    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 تعليل 4 درجات

    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 تعليل 3 درجات

    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 تطبيق 4 درجات

    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 تعليل 3 درجات

    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?