Data Structures and Algorithms (DSA27) · Week 6

Stacks (Lecture 06)

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

    "LIFO" means:

  2. W6-M02 Apply

    push(1), push(2), push(3), pop(), push(4), pop(). What does the next pop() return?

  3. W6-M03 Explain

    A stack built on a dynamic array should keep its top at:

  4. W6-M04 Explain

    A stack built on a singly linked list should keep its top at:

  5. W6-M05 Recall

    Stack().pop() on an empty stack:

  6. W6-M06 Explain

    A stack on an array with its top at index 0. push costs:

  7. W6-M07 Apply

    Why is "([)]" not balanced?

  8. W6-M08 Apply

    Which check makes is_balanced(")") return False?

  9. W6-M09 Apply

    The postfix form of 3 + 4 * 2 is:

  10. W6-M10 Apply

    The postfix form of ( 3 + 4 ) * 2 is:

  11. W6-M11 Apply

    Evaluating the postfix 8 3 - gives:

  12. W6-M12 Apply

    Evaluating the postfix 2 3 4 * + gives:

  13. W6-M13 Recall

    In the shunting-yard algorithm, when an operator arrives, it first pops to the output every stacked operator with:

  14. W6-M14 Explain

    Why does an operator of equal precedence also leave the stack?

  15. W6-M15 Apply

    The postfix form of 8 - 3 - 2 is:

  16. W6-M16 Apply

    The worst-case extra space of is_balanced on a text of length n is:

  17. W6-M17 Explain

    To turn a recursive algorithm into a loop, in general you need:

  18. W6-M18 Recall

    In an undo/redo system with two stacks, what happens to the redo stack when the user performs a new action?

  19. W6-M19 Apply

    n pushes onto the Stack of dsa/stack.py (built on a DynamicArray) cost in total:

  20. W6-M20 Recall

    Which of these is not a natural stack application?

  21. W6-M21 Apply

    While is_balanced("(((())))") runs, the largest number of items on the stack at once is:

  22. W6-M22 Recall

    peek() differs from pop() in that it:

  23. W6-M23 Apply

    The shunting-yard algorithm converts 2 * 3 + 4 to:

  24. W6-M24 Apply

    The shunting-yard algorithm converts ( 1 + 2 ) * ( 3 - 4 ) / 5 to:

  25. W6-M25 Explain

    A student's infix_to_postfix pops an operator only when the stacked one has strictly higher precedence (> instead of >=). Their calculator, evaluate_postfix(infix_to_postfix(...)), now gives 8 - 3 - 2 as:

  26. W6-M26 Explain

    With the same > bug, which of these inputs is still converted correctly?

  27. W6-M27 Apply

    evaluate_postfix(["6", "2", "/", "3", "*"]) returns:

  28. W6-M28 Explain

    A buggy evaluate_postfix computes (first value popped) op (second value popped): the operands the wrong way round. On which input does it still give the right answer?

  29. W6-M29 Apply

    evaluate_postfix(["3", "4"]):

  30. W6-M30 Recall

    evaluate_postfix(["1", "0", "/"]):

  31. W6-M31 Explain

    A version of is_balanced ends with return True instead of return openers.is_empty(). Which input does it get wrong?

  32. W6-M32 Recall

    A version of is_balanced pops for every closer without first checking that the stack is non-empty. On "())" it:

  33. W6-M33 Apply

    is_balanced("a(b)c)d(") returns False. Which check fails first?

  34. W6-M34 Explain

    is_balanced("print('(')"), a line of valid Python, returns:

  35. W6-M35 Explain

    Most pushes onto the Stack of dsa/stack.py are instant, but now and then one push takes much longer. Why?

  36. W6-M36 Recall

    Which stack gives worst-case O(1) for every push, not just amortised O(1)?

  37. W6-M37 Recall

    s = Stack([1, 2, 3]). What does s.pop() return?

  38. W6-M38 Explain

    Two peek() calls in a row, with nothing in between, return two different values. The most likely bug is that peek:

  39. W6-M39 Apply

    An editor with an undo stack and a redo stack. The user types A, types B, types C, then: undo, undo, redo, types D. What is on the two stacks (bottom → top)?

  40. W6-M40 Explain

    nested_sum with your own Stack instead of recursion. Its real advantage in Python is that:

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. W6-E1 Explain 4 marks

    State the Stack ADT with the cost of each operation. Describe two honest implementations, compare them, and describe one design that passes every test but is not honest.

  2. W6-E2 Apply 4 marks

    Describe the stack algorithm for balanced brackets. Give an example of each of the three ways a text can be unbalanced, and the check that catches it.

  3. W6-E3 Apply 3 marks

    Explain how a stack evaluates a postfix expression. Why does the order in which the two operands are popped matter, and for which operators?

  4. W6-E4 Explain 4 marks

    Explain the shunting-yard algorithm, and trace it on 2 * ( 3 + 4 ) - 5. Why must an operator of equal precedence be popped?

  5. W6-E5 Explain 3 marks

    "Recursion is a stack." Explain, and describe how you would rewrite a recursive algorithm without recursion. When is that worth doing in Python?

  6. W6-E6 Explain 4 marks

    A student's infix_to_postfix pops an operator only on strictly higher precedence (> instead of >=). Give one input that it still converts correctly and one that it gets wrong, with both postfix results and their values. Explain why the tests can miss this bug, and name an operator for which > is the right rule.

  7. W6-E7 Apply 3 marks

    Run the whole calculator on ( 8 - 2 ) / ( 1 + 2 ) * 4: convert it to postfix with the shunting-yard algorithm, then evaluate the postfix with a stack, showing the stack after each operator. Why is the answer a float?

  8. W6-E8 Explain 4 marks

    Compare a stack on a dynamic array (top at the end) with a stack on a singly linked list (top at the head): the cost of push in the worst case and amortised, the memory per item, and memory locality. The lecture's timings found the linked stack faster in this course. Why, and would that still hold for Python's own list? Which would you choose for a program that must never pause?

  9. W6-E9 Apply 3 marks

    Design undo and redo for a text editor with two stacks. Say what each of action, Undo and Redo does to the two stacks, and show both stacks after: type A, type B, type C, undo, undo, redo, type D. What would go wrong if a new action did not clear the redo stack?