mirror of
https://github.com/yangshun/tech-interview-handbook.git
synced 2025-07-30 21:53:28 +08:00
contents: revamp basic algo content
This commit is contained in:
@ -1,30 +1,17 @@
|
||||
---
|
||||
id: interval
|
||||
title: Interval
|
||||
toc_max_heading_level: 2
|
||||
---
|
||||
|
||||
## Notes
|
||||
## Introduction
|
||||
|
||||
Interval questions are questions where you are given an array of two-element arrays (an interval) and the two values represent a start and an end value. Interval questions are considered part of the array family but they involve some common techniques hence they are extracted out to this special section of their own.
|
||||
Interval questions are a subset of [array](./array.md) questions where you are given an array of two-element arrays (an interval) and the two values represent a start and an end value. Interval questions are considered part of the array family but they involve some common techniques hence they are extracted out to this special section of their own.
|
||||
|
||||
An example interval array: `[[1, 2], [4, 7]]`.
|
||||
|
||||
Interval questions can be tricky to those who have not tried them before because of the sheer number of cases to consider when they overlap.
|
||||
|
||||
Do clarify with the interviewer whether `[1, 2]` and `[2, 3]` are considered overlapping intervals as it affects how you will write your equality checks.
|
||||
|
||||
A common routine for interval questions is to sort the array of intervals by each interval's starting value.
|
||||
|
||||
Be familiar with writing code to check if two intervals overlap and merging two overlapping intervals:
|
||||
|
||||
```py
|
||||
def is_overlap(a, b):
|
||||
return a[0] < b[1] and b[0] < a[1]
|
||||
|
||||
def merge_overlapping_intervals(a, b):
|
||||
return [min(a[0], b[0]), max(a[1], b[1])]
|
||||
```
|
||||
|
||||
## Corner cases
|
||||
|
||||
- Single interval
|
||||
@ -32,12 +19,39 @@ def merge_overlapping_intervals(a, b):
|
||||
- An interval totally consumed within another interval
|
||||
- Duplicate intervals
|
||||
|
||||
## Recommended LeetCode questions
|
||||
## Things to look out for during interviews
|
||||
|
||||
- Do clarify with the interviewer whether `[1, 2]` and `[2, 3]` are considered overlapping intervals as it affects how you will write your equality checks.
|
||||
|
||||
## Techniques
|
||||
|
||||
### Sort the array of intervals by its starting point
|
||||
|
||||
A common routine for interval questions is to sort the array of intervals by each interval's starting value. This step is crucial to solving the [Merge Intervals](https://leetcode.com/problems/merge-intervals/) question.
|
||||
|
||||
### Checking if two intervals overlap
|
||||
|
||||
Be familiar with writing code to check if two intervals overlap.
|
||||
|
||||
```py
|
||||
def is_overlap(a, b):
|
||||
return a[0] < b[1] and b[0] < a[1]
|
||||
```
|
||||
|
||||
### Merging two intervals
|
||||
|
||||
```py
|
||||
def merge_overlapping_intervals(a, b):
|
||||
return [min(a[0], b[0]), max(a[1], b[1])]
|
||||
```
|
||||
|
||||
## Recommended questions
|
||||
|
||||
- [Insert Interval](https://leetcode.com/problems/insert-interval/)
|
||||
- [Merge Intervals](https://leetcode.com/problems/merge-intervals/)
|
||||
- [Insert Interval](https://leetcode.com/problems/insert-interval/)
|
||||
- [Non-overlapping Intervals](https://leetcode.com/problems/non-overlapping-intervals/)
|
||||
- [Meeting Rooms (LeetCode Premium)](https://leetcode.com/problems/meeting-rooms/) and [Meeting Rooms II (LeetCode Premium)](https://leetcode.com/problems/meeting-rooms-ii/)
|
||||
- [Meeting Rooms (LeetCode Premium)](https://leetcode.com/problems/meeting-rooms/)
|
||||
- [Meeting Rooms II (LeetCode Premium)](https://leetcode.com/problems/meeting-rooms-ii/)
|
||||
|
||||
## Recommended courses
|
||||
|
||||
|
Reference in New Issue
Block a user