Recommended resources
The freely available resources this lesson will direct you to.
- Master the Interval Coding Pattern: Crush FAANG technical Interviews with This Crucial Technique! 🔥www.youtube.com
- How to Instantly Recognize Leetcode Patterns (500 problems later)www.youtube.com
- GitHub - nirajkvinit/StructWeave-Algorithms: 900+ DSA practice problems with 17 algorithm patterns that solve 90% of coding interviews. Pattern-first learning. Free interview prep. Golang, Python, Rust, TypeScript, SOLID · GitHubgithub.com
- How to Identify Which Pattern to Use | DSA | AlgoMaster.ioalgomaster.io
Welcome back! In our last lesson, we started building a systematic framework for tackling unseen algorithm problems, focusing on how to choose between common array patterns like hashing, sliding windows, and prefix sums. You learned to deconstruct a problem's statement, constraints, and keywords to make a confident, evidence-based choice.
In this lesson, we will expand that framework to a new and equally important category of problems: those involving ordered data, intervals, and optimization. You'll learn to identify the subtle clues that point towards powerful techniques like sorting, binary search, heaps, and greedy sweeps. The goal is to move from "What pattern should I use?" to "The problem's structure clearly indicates that this pattern is the most logical fit." This skill is essential for solving a vast range of LeetCode Medium problems related to scheduling, data analysis, and searching.
The Decision Framework: Ordered Data and Intervals
When faced with a problem involving sorted arrays, intervals of time, or finding "optimal" values like the "k-th largest" or "minimum possible," a different set of patterns comes into play. Just as before, we can use signals from the problem statement to guide our selection.
Let's refine the "OA Translation Loop" we've seen before for this new set of patterns.

The key is to look for specific signals in the problem's input type, constraints, and keywords. The resource below provides an excellent set of tables that map these signals to candidate patterns.
How to Identify Which Pattern to Use | DSA | AlgoMaster.io
This article from AlgoMaster.io provides a concise, signal-based guide for pattern recognition. It's a great reference for building the mental checklist we're developing.
As you read, focus on these key tables: Input Type to Patterns: Notice how "Sorted array" and "Intervals" immediately narrow down the possibilities. Constraints to Complexity: Reinforce the idea that an O(N \log N) complexity, typical for sorting and heap-based solutions, is feasible for inputs up to around 100,000 elements. Keywords to Patterns: Pay close attention to phrases like "Sorted array," "Merge intervals," "kth largest/smallest," and "Top k." These are extremely reliable triggers. Summary Table: This provides a compact summary linking problem types to patterns and complexities.
Based on this reading, we can formulate a decision framework:
Let's see this framework in action with a classic interval problem.
Worked Example: Merge Intervals (LeetCode 56)
Problem: Given an array of intervals where intervals[i] = [start_i, end_i], merge all overlapping intervals, and return an array of the non-overlapping intervals that cover all the intervals in the input.
Example:
Input: intervals = [[1,3],[2,6],[8,10],[15,18]]
Output: [[1,6],[8,10],[15,18]]
Explanation: Since intervals [1,3] and [2,6] overlap, they are merged into [1,6].
1. Deconstruction and Pattern Selection:
- Input Type: An array of intervals. The framework immediately points to Sort + Greedy Sweep.
- Keywords: "merge," "overlapping intervals." This is a textbook trigger for the same pattern.
- Constraints: A typical LeetCode constraint might be
intervals.length <= 10^4. An approach, where you compare every interval with every other interval, would be too slow ( operations). An solution is required, which perfectly matches the complexity of sorting.
Our diagnosis is clear: the most promising approach is to sort the intervals and then process them in a single, greedy pass.
2. The Solution Logic
The following video provides an excellent, detailed walkthrough of this exact problem. It explains the core insight: if you sort the intervals by their start times, you only ever need to consider merging the current interval with the previous merged interval.
Watch this segment from Destination FAANG's video to understand the "Merge Intervals" problem. The presenter does a great job of visualizing why sorting is the key.
Please watch the detailed explanation of the Merge Intervals problem. Pay close attention to: The condition for an overlap after sorting. Why sorting is crucial and how it turns an O(N^2) problem into an O(N \log N) one. The step-by-step merge process on a sorted list.
The core algorithm, as explained in the video, is:
- Sort the intervals based on their start times. This costs .
- Initialize an empty
mergedarray and add the first interval from your sorted list. - Iterate through the remaining sorted intervals, starting from the second one.
- For each
currentinterval, look at thelastinterval in yourmergedarray. - Greedy Choice:
- If
current.start <= last.end(they overlap), updatelast.end = max(last.end, current.end). - If they do not overlap, add
currentas a new interval to themergedarray.
- If
- Return the
mergedarray.
This Sort + Greedy Sweep is the canonical pattern for a vast range of interval problems.
Contrasting Patterns: Heaps and Binary Search
The learning outcome also includes heaps and binary search. Let's briefly see how to recognize problems where these are the right tools.
When to Use a Heap
Problem Hint: The problem asks for the "k-th largest/smallest" element or the "top k" items from a collection.
Example: Find the k-th largest element in an unsorted array nums.
- Naive: Sort
nums() and pick the element at indexn-k. - Heap-based: Maintain a min-heap of size
k. Iterate throughnums. For each number, if it's larger than the smallest number in the heap (the root), pop the root and push the new number. After processing allNnumbers, the root of the heap is the k-th largest element. - Why? This approach has a time complexity of . This is more efficient than sorting if
kis much smaller thanN. More importantly, it's the specific pattern designed for this type of "top k" query.

When to Use Binary Search on the Answer
Problem Hint: The problem asks to find the minimum value that satisfies a condition, or the maximum value that is still possible. Often, the phrasing is "minimize the maximum" or "maximize the minimum."
Example: "Capacity To Ship Packages Within D Days" (LeetCode 1011). You need to find the minimum ship capacity to ship all packages in D days.
- Insight: The problem isn't about searching a sorted array. It's about searching the space of possible answers (the capacity). This space is monotonic: if a ship with capacity
Cworks, any capacityC' > Cwill also work. - Binary Search Approach:
- Define a search range for the answer (e.g., from
max(weights)tosum(weights)). - Binary search within this range. For each
midcapacity, check if it'sfeasible(i.e., can you ship all packages inDdays with this capacity?). - If
feasible(mid)is true, it meansmidmight be our answer, but maybe an even smaller capacity also works. So, we try the lower half:high = mid. - If
feasible(mid)is false,midis too small. We must increase it:low = mid + 1.
- Define a search range for the answer (e.g., from
- This pattern effectively finds the "lower bound" of all feasible capacities.
Key Takeaways
Today, you've expanded your problem-solving toolkit to include patterns for ordered data and intervals. The selection process remains the same: use clues from the problem to guide your choice.
- Intervals Signal
Sort + Greedy: Problems involving merging, scheduling, or finding overlaps in[start, end]pairs almost always benefit from sorting by the start time, followed by a single greedy pass. - "Top K" or "K-th Largest" Signals
Heap: When you need to find the largest or smallestkelements from a larger collection, a heap is the purpose-built data structure, often providing a more optimal solution than full sorting. - "Minimize/Maximize a Value" Signals
Binary Search on the Answer: If a problem has a monotonic property (ifxworks, anything better thanxalso works), you can often binary search for the optimal valuexin the range of possible answers.
In the next lesson, we will apply this same pattern-selection mindset to problems involving linked lists, stacks, and trees, focusing on how to define the state you need to preserve during traversal or pointer manipulation.