Day 2 is in the books, folks!
Yesterday was about binary trees.
Today, I switched gears to Intervals.
Interval problems usually boil down to one core pattern:
Sort by Start Time.
If you don’t sort, you are likely trying to solve it in O(n²), which won’t pass.
The Python sorting check
I had a quick “syntax check” moment today.
When dealing with raw arrays [1,3,2,4], sorting is easy.
But when the input is given as a list of Objects (like class Interval), I always blank out on the Python syntax for a second.
For future reference:
Python
# The clean way to sort objects in Python
intervals.sort(key=lambda x: x.start)It’s a small thing, but fumbling this in a live coding round is embarrassing.
The Atlassian Variations
I focused on questions that have been tagged for Atlassian.
You know how much they love modifying standard interval problems slightly to throw you off? ;)
1. The Classic: Merge Intervals
Input: [[1,3],[3,5],[8,10]]
Output: [[1,5],[8,10]]
This is standard LeetCode 56.
The logic is simple:
Sort them. If the current interval starts before the previous one ends, merge them.
class Solution:
def merge(self, intervals: List[List[int]]) -> List[List[int]]:
intervals.sort() # Sort by start time
merged = []
prev = intervals[0]
for i in range(1, len(intervals)):
if intervals[i][0] <= prev[1]: # Overlap
prev[1] = max(prev[1], intervals[i][1]) # Merge
else:
merged.append(prev)
prev = intervals[i]
merged.append(prev)
return merged2. The Variation: Ranges with Minimum 2 Overlaps
This was the interesting one.
Input: [[1,3],[3,4],[5,8],[6,10],[9,12]]
Output: [[6,8],[9,10]]
This isn’t just “merge if they touch.”
You need to find the specific timeline segments where the “meeting count” is ≥2
- Logic: Line Sweep algorithm. You break every interval into points:
(start, +1)and(end, -1). Sort the points, then iterate through them maintaining acount. Whenevercount >= 2, you are inside a target range.
Meeting Rooms 1/Meeting Rooms II
Solved the classic generic version as well.
It’s essentially the same logic as the problem above, yk, finding the maximum value of concurrent meetings.
The Life Log
- Activity: 30-minutes walk.
- Diet: Needs fixing. I’m doing the work on screen, but my nutrition is lagging. This war requires better fuel. I’m open to help here.
- Random: I shaved today for the first time in an year