Embercode
✕
Stacks and QueuesQuick quiz

Trapping rain water

6 questions. Covers Trapping rain water with a stack.

Questions 1 to 5 of 6

  1. 1. How much water does the list 3 0 0 0 3 hold?
  2. 2. What does the line if (walls.empty()) break; in trap_stack prevent?
  3. 3. Put the steps the stack method uses for one new bar i, from the lesson, in the order they happen.

    Use the arrows to put the steps in order.

    1. 1.Set left to the new top, width to i - left - 1 and level to the shorter of h[left] and h[i], then add width times (level minus h[bottom])
    2. 2.Push i onto the stack
    3. 3.If the stack is now empty, stop, because no wall is on the left
    4. 4.While the stack is not empty and bar i is taller than the bar on top, pop the top bar and call it bottom
  4. 4. The stack method runs on the list 4 2 0 3 2 5. Put these bar pops in the order they happen.

    Use the arrows to put the steps in order.

    1. 1.Pop bar 1 (height 2) when bar 3 arrives
    2. 2.Pop bar 4 (height 2) when bar 5 (height 5) arrives
    3. 3.Pop bar 3 (height 3) when bar 5 arrives
    4. 4.Pop bar 2 (height 0) when bar 3 (height 3) arrives
  5. 5. True or false: the two-pointer method moves the pointer on the taller side first.

3 question(s) left on this page.