Embercode
✕
Stacks and QueuesQuick quiz

Stack and queue bug hunt

8 questions. Covers Debugging lab: Python stack and queue bugs.

Questions 1 to 5 of 8

  1. 1. This is_full method is from a stack built on a list of capacity 3. What is wrong with it? ``python def is_full(self): return self.top == len(self.data) ``
  2. 2. A ring queue uses these two tests. What is the bug? ``python def is_empty(self): return self.front == self.rear def is_full(self): return self.front == self.rear ``
  3. 3. A linked-list queue has this dequeue. It is run on a queue with one item. What is the bug? ``python def dequeue(self): if self.is_empty(): raise IndexError("queue is empty") value = self.head.value self.head = self.head.next return value ``
  4. 4. A bracket check should return True for (). What is the bug in this part of the loop? ``python open_bracket = stack[-1] if (ch == ")" and open_bracket != "(") or (ch == "]" and open_bracket != "["): return False ``
  5. 5. Using the fixed ArrayStack from the first bug (capacity 3), what does this code print? ``python s = ArrayStack(3) s.push(4) s.push(9) s.push(2) print(s.is_full()) print(s.pop()) s.push(7) print(s.pop()) print(s.pop()) ``

5 question(s) left on this page.