Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Build confidence with an intuition-first walkthrough focused on array fundamentals.
You are given a 2D integer array ranges and two integers left and right. Each ranges[i] = [starti, endi] represents an inclusive interval between starti and endi.
Return true if each integer in the inclusive range [left, right] is covered by at least one interval in ranges. Return false otherwise.
An integer x is covered by an interval ranges[i] = [starti, endi] if starti <= x <= endi.
Example 1:
Input: ranges = [[1,2],[3,4],[5,6]], left = 2, right = 5 Output: true Explanation: Every integer between 2 and 5 is covered: - 2 is covered by the first range. - 3 and 4 are covered by the second range. - 5 is covered by the third range.
Example 2:
Input: ranges = [[1,10],[10,20]], left = 21, right = 21 Output: false Explanation: 21 is not covered by any range.
Constraints:
1 <= ranges.length <= 501 <= starti <= endi <= 501 <= left <= right <= 50Problem summary: You are given a 2D integer array ranges and two integers left and right. Each ranges[i] = [starti, endi] represents an inclusive interval between starti and endi. Return true if each integer in the inclusive range [left, right] is covered by at least one interval in ranges. Return false otherwise. An integer x is covered by an interval ranges[i] = [starti, endi] if starti <= x <= endi.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Hash Map
[[1,2],[3,4],[5,6]] 2 5
[[1,10],[10,20]] 21 21
find-maximal-uncovered-ranges)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1893: Check if All the Integers in a Range Are Covered
class Solution {
public boolean isCovered(int[][] ranges, int left, int right) {
int[] diff = new int[52];
for (int[] range : ranges) {
int l = range[0], r = range[1];
++diff[l];
--diff[r + 1];
}
int s = 0;
for (int i = 0; i < diff.length; ++i) {
s += diff[i];
if (s <= 0 && left <= i && i <= right) {
return false;
}
}
return true;
}
}
// Accepted solution for LeetCode #1893: Check if All the Integers in a Range Are Covered
func isCovered(ranges [][]int, left int, right int) bool {
diff := [52]int{}
for _, e := range ranges {
l, r := e[0], e[1]
diff[l]++
diff[r+1]--
}
s := 0
for i, x := range diff {
s += x
if s <= 0 && left <= i && i <= right {
return false
}
}
return true
}
# Accepted solution for LeetCode #1893: Check if All the Integers in a Range Are Covered
class Solution:
def isCovered(self, ranges: List[List[int]], left: int, right: int) -> bool:
diff = [0] * 52
for l, r in ranges:
diff[l] += 1
diff[r + 1] -= 1
s = 0
for i, x in enumerate(diff):
s += x
if s <= 0 and left <= i <= right:
return False
return True
// Accepted solution for LeetCode #1893: Check if All the Integers in a Range Are Covered
struct Solution;
use std::collections::HashSet;
impl Solution {
fn is_covered(ranges: Vec<Vec<i32>>, left: i32, right: i32) -> bool {
let mut a = HashSet::new();
let mut b = HashSet::new();
for i in left..=right {
b.insert(i);
}
for r in ranges {
let start = r[0];
let end = r[1];
for i in start..=end {
if b.contains(&i) {
a.insert(i);
}
}
}
a == b
}
}
#[test]
fn test() {
let ranges = vec_vec_i32![[1, 2], [3, 4], [5, 6]];
let left = 2;
let right = 5;
let res = true;
assert_eq!(Solution::is_covered(ranges, left, right), res);
let ranges = vec_vec_i32![[1, 10], [10, 20]];
let left = 21;
let right = 21;
let res = false;
assert_eq!(Solution::is_covered(ranges, left, right), res);
}
// Accepted solution for LeetCode #1893: Check if All the Integers in a Range Are Covered
function isCovered(ranges: number[][], left: number, right: number): boolean {
const diff: number[] = Array(52).fill(0);
for (const [l, r] of ranges) {
++diff[l];
--diff[r + 1];
}
let s = 0;
for (let i = 0; i < diff.length; ++i) {
s += diff[i];
if (s <= 0 && left <= i && i <= right) {
return false;
}
}
return true;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.