Skip to content
Search lessons, topics, tests…
Esc

    ↑ ↓ moveEnter openEsc close

    Coding problem

    The highest reading in every window

    HardAbout 40 minFull AccessLearn it: Queues and Deques in C#: Queue<T>, Two-Stack Queues and Sliding Window Maximum

    The task

    A sensor sends one reading per second, and a dashboard shows the highest reading of the last k seconds, updated every second.

    Complete WindowMaxima(int[] values, int k). For every run of k neighbouring values, return its maximum, in window order. 1 ≤ k ≤ values.Length, so there are values.Length − k + 1 windows.

    Signature

    public static int[] WindowMaxima(int[] values, int k)

    Examples

    • [5, 1, 1, 1, 5], k = 3 → [5, 1, 5]. The first 5 leaves after the first window, and the last 5 arrives only in the third.
    • [1, 2, 3, 4], k = 2 → [2, 3, 4]. Each window's maximum is its newer value.

    Limits: Up to 600,000 values; k can be as large as the array.

    The tests check: one value, k = 1, one window over everything, rising, falling and equal values, negative values, a maximum that leaves the window, and 600,000 falling values with k = 300,000 in time.

    Hints

      Your code

      C#Solution.csLoading the editor…

      Already included: using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net.Http; using System.Threading; using System.Threading.Tasks;

      public static class Solution
      {
          // For every run of k neighbouring values, its maximum, in window order.
          public static int[] WindowMaxima(int[] values, int k)
          {
              throw new NotImplementedException();
          }
      }

      This problem comes with TechInterviewNotes Full Access

      Unlock all paid content on TechInterviewNotes.