X interview question

Moving window sorting LC question.