Windows, stacks and binary search

Patterns for sequences: sliding windows over contiguous parts, stacks and queues for nesting and next-greater problems, and binary search on lists and on answers.

About 5 hours of study

Learning objectives

Lessons

  1. 1

    Sliding windows

    A window over a list or string that slides instead of restarting: fixed-size windows, the longest substring without repeats, and the smallest window that meets a condition.

  2. 2

    Stacks and queues

    Stacks for matching brackets and for monotonic-stack problems like the next greater element; queues and deques for breadth-first order and sliding-window maximums.

  3. 3

    Binary search patterns

    Binary search beyond "find x": the first position that passes a test, searching a rotated list, and binary search on the answer itself.

Module practice

Module quiz

1. What makes a variable sliding window run in O(n)?
2. For a window of size k that moves one step, what changes in the sum?
3. The next greater element for each position is found with...
4. Which operation makes list.pop(0) a slow queue?
5. In first_true, the test is False for small values and True later. If mid fails, you set...
6. Searching the smallest capacity that delivers in D days is binary search on what?

‹ Interview foundationsLinked lists and trees ›