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 0-indexed 2D integer array nums representing the coordinates of the cars parking on a number line. For any index i, nums[i] = [starti, endi] where starti is the starting point of the ith car and endi is the ending point of the ith car.
Return the number of integer points on the line that are covered with any part of a car.
Example 1:
Input: nums = [[3,6],[1,5],[4,7]] Output: 7 Explanation: All the points from 1 to 7 intersect at least one car, therefore the answer would be 7.
Example 2:
Input: nums = [[1,3],[5,8]] Output: 7 Explanation: Points intersecting at least one car are 1, 2, 3, 5, 6, 7, 8. There are a total of 7 points, therefore the answer would be 7.
Constraints:
1 <= nums.length <= 100nums[i].length == 21 <= starti <= endi <= 100Problem summary: You are given a 0-indexed 2D integer array nums representing the coordinates of the cars parking on a number line. For any index i, nums[i] = [starti, endi] where starti is the starting point of the ith car and endi is the ending point of the ith car. Return the number of integer points on the line that are covered with any part of a car.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Hash Map
[[3,6],[1,5],[4,7]]
[[1,3],[5,8]]
merge-intervals)meeting-rooms)meeting-rooms-ii)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2848: Points That Intersect With Cars
class Solution {
public int numberOfPoints(List<List<Integer>> nums) {
int[] d = new int[102];
for (var e : nums) {
int start = e.get(0), end = e.get(1);
++d[start];
--d[end + 1];
}
int ans = 0, s = 0;
for (int x : d) {
s += x;
if (s > 0) {
++ans;
}
}
return ans;
}
}
// Accepted solution for LeetCode #2848: Points That Intersect With Cars
func numberOfPoints(nums [][]int) (ans int) {
d := [102]int{}
for _, e := range nums {
start, end := e[0], e[1]
d[start]++
d[end+1]--
}
s := 0
for _, x := range d {
s += x
if s > 0 {
ans++
}
}
return
}
# Accepted solution for LeetCode #2848: Points That Intersect With Cars
class Solution:
def numberOfPoints(self, nums: List[List[int]]) -> int:
m = 102
d = [0] * m
for start, end in nums:
d[start] += 1
d[end + 1] -= 1
return sum(s > 0 for s in accumulate(d))
// Accepted solution for LeetCode #2848: Points That Intersect With Cars
fn number_of_points(nums: Vec<Vec<i32>>) -> i32 {
let mut nums = nums;
nums.sort_by(|a, b| {
if a[0] == b[0] {
b[1].cmp(&a[1])
} else {
a[0].cmp(&b[0])
}
});
let mut ret = 0;
let mut s = nums[0][0];
let mut e = nums[0][1];
for v in nums.into_iter().skip(1) {
if v[0] > e {
ret += e - s + 1;
s = v[0];
e = v[1];
} else {
s = std::cmp::min(s, v[0]);
e = std::cmp::max(e, v[1]);
}
}
ret += e - s + 1;
ret
}
fn main() {
let nums = vec![vec![1, 3], vec![5, 8]];
let ret = number_of_points(nums);
println!("ret={ret}");
}
#[test]
fn test_number_of_points() {
{
let nums = vec![vec![3, 6], vec![1, 5], vec![4, 7]];
let ret = number_of_points(nums);
assert_eq!(ret, 7);
}
{
let nums = vec![vec![1, 3], vec![5, 8]];
let ret = number_of_points(nums);
assert_eq!(ret, 7);
}
{
let nums = vec![vec![1, 2], vec![3, 4], vec![5, 6]];
let ret = number_of_points(nums);
assert_eq!(ret, 6);
}
{
let nums = vec![vec![1, 10], vec![3, 4], vec![5, 6]];
let ret = number_of_points(nums);
assert_eq!(ret, 10);
}
}
// Accepted solution for LeetCode #2848: Points That Intersect With Cars
function numberOfPoints(nums: number[][]): number {
const d: number[] = Array(102).fill(0);
for (const [start, end] of nums) {
++d[start];
--d[end + 1];
}
let ans = 0;
let s = 0;
for (const x of d) {
s += x;
ans += s > 0 ? 1 : 0;
}
return ans;
}
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.