Embercode
✕
Built-in CollectionsQuick quiz

Monotonic deque with collections.deque

6 questions. Covers Monotonic deque in Python.

Questions 1 to 5 of 6

  1. 1. What does this program print? ``python from collections import deque def window_min(nums, k): result = [] dq = deque() for i in range(len(nums)): if dq and dq[0] <= i - k: dq.popleft() while dq and nums[dq[-1]] >= nums[i]: dq.pop() dq.append(i) if i >= k - 1: result.append(nums[dq[0]]) return result print(window_min([5, 3, 8, 1, 6], 3)) ``
  2. 2. What does this program print? ``python from collections import deque def window_max(nums, k): result = [] dq = deque() for i in range(len(nums)): if dq and dq[0] <= i - k: dq.popleft() while dq and nums[dq[-1]] <= nums[i]: dq.pop() dq.append(i) if i >= k - 1: result.append(nums[dq[0]]) return result print(window_max([7, 1, 6, 2, 8, 3], 3)) ``
  3. 3. What does this program print? ``python from collections import deque def window_max(nums, k): result = [] dq = deque() for i in range(len(nums)): if dq and dq[0] <= i - k: dq.popleft() while dq and nums[dq[-1]] <= nums[i]: dq.pop() dq.append(i) if i >= k - 1: result.append(nums[dq[0]]) return result print(window_max([2, 9, 4, 9, 1], 5)) ``
  4. 4. For each new index i, the window_max loop body runs four steps. Put them in the order the code runs them.

    Use the arrows to put the steps in order.

    1. 1.Remove indices from the back while the value there is at most nums[i]
    2. 2.Add i at the back of the deque
    3. 3.Once i is at least k - 1, write nums[dq[0]] as the answer for this window
    4. 4.Drop the front index if it is at most i - k, because it has left the window
  5. 5. What single change turns window_max into window_min?

4 question(s) left on this page.