You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
87 lines
3.1 KiB
87 lines
3.1 KiB
5 years ago
|
---
|
||
|
id: interval
|
||
3 years ago
|
title: Interval cheatsheet for coding interviews
|
||
|
description: Interval study guide for coding interviews, including practice questions, techniques, time complexity, and recommended resources
|
||
|
keywords:
|
||
|
[
|
||
|
interval coding interview study guide,
|
||
|
interval tips for coding interviews,
|
||
|
interval practice questions,
|
||
|
interval useful techniques,
|
||
|
interval time complexity,
|
||
|
interval recommended study resources,
|
||
|
]
|
||
|
sidebar_label: Interval
|
||
3 years ago
|
toc_max_heading_level: 2
|
||
5 years ago
|
---
|
||
7 years ago
|
|
||
3 years ago
|
<head>
|
||
|
<meta property="og:image" content="https://www.techinterviewhandbook.org/social/algorithms/algorithms/algorithms-interval.png" />
|
||
|
</head>
|
||
|
|
||
3 years ago
|
## Introduction
|
||
5 years ago
|
|
||
3 years ago
|
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.
|
||
5 years ago
|
|
||
|
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.
|
||
|
|
||
3 years ago
|
## Things to look out for during interviews
|
||
|
|
||
|
- Clarify with the interviewer whether `[1, 2]` and `[2, 3]` are considered overlapping intervals as it affects how you will write your equality checks.
|
||
|
- Clarify whether an interval of `[a, b]` will strictly follow `a` < `b` (`a` is smaller than `b`)
|
||
|
|
||
3 years ago
|
## Corner cases
|
||
|
|
||
3 years ago
|
- No intervals
|
||
3 years ago
|
- Single interval
|
||
3 years ago
|
- Two intervals
|
||
3 years ago
|
- Non-overlapping intervals
|
||
|
- An interval totally consumed within another interval
|
||
3 years ago
|
- Duplicate intervals (exactly the same start and end)
|
||
|
- Intervals which start right where another interval ends - `[[1, 2], [2, 3]]`
|
||
3 years ago
|
|
||
|
## 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.
|
||
5 years ago
|
|
||
|
```py
|
||
|
def is_overlap(a, b):
|
||
|
return a[0] < b[1] and b[0] < a[1]
|
||
3 years ago
|
```
|
||
5 years ago
|
|
||
3 years ago
|
### Merging two intervals
|
||
|
|
||
|
```py
|
||
5 years ago
|
def merge_overlapping_intervals(a, b):
|
||
|
return [min(a[0], b[0]), max(a[1], b[1])]
|
||
|
```
|
||
|
|
||
3 years ago
|
## Essential questions
|
||
|
|
||
|
_These are essential questions to practice if you're studying for this topic._
|
||
5 years ago
|
|
||
|
- [Merge Intervals](https://leetcode.com/problems/merge-intervals/)
|
||
3 years ago
|
- [Insert Interval](https://leetcode.com/problems/insert-interval/)
|
||
3 years ago
|
|
||
|
## Recommended practice questions
|
||
|
|
||
|
_These are recommended questions to practice after you have studied for the topic and have practiced the essential questions._
|
||
|
|
||
5 years ago
|
- [Non-overlapping Intervals](https://leetcode.com/problems/non-overlapping-intervals/)
|
||
3 years ago
|
- [Meeting Rooms (LeetCode Premium)](https://leetcode.com/problems/meeting-rooms/)
|
||
|
- [Meeting Rooms II (LeetCode Premium)](https://leetcode.com/problems/meeting-rooms-ii/)
|
||
3 years ago
|
|
||
|
## Recommended courses
|
||
|
|
||
|
import AlgorithmCourses from '../\_courses/AlgorithmCourses.md'
|
||
|
|
||
|
<AlgorithmCourses />
|