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 logs where each logs[i] = [birthi, deathi] indicates the birth and death years of the ith person.
The population of some year x is the number of people alive during that year. The ith person is counted in year x's population if x is in the inclusive range [birthi, deathi - 1]. Note that the person is not counted in the year that they die.
Return the earliest year with the maximum population.
Example 1:
Input: logs = [[1993,1999],[2000,2010]] Output: 1993 Explanation: The maximum population is 1, and 1993 is the earliest year with this population.
Example 2:
Input: logs = [[1950,1961],[1960,1971],[1970,1981]] Output: 1960 Explanation: The maximum population is 2, and it had happened in years 1960 and 1970. The earlier year between them is 1960.
Constraints:
1 <= logs.length <= 1001950 <= birthi < deathi <= 2050Problem summary: You are given a 2D integer array logs where each logs[i] = [birthi, deathi] indicates the birth and death years of the ith person. The population of some year x is the number of people alive during that year. The ith person is counted in year x's population if x is in the inclusive range [birthi, deathi - 1]. Note that the person is not counted in the year that they die. Return the earliest year with the maximum population.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array
[[1993,1999],[2000,2010]]
[[1950,1961],[1960,1971],[1970,1981]]
shifting-letters-ii)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1854: Maximum Population Year
class Solution {
public int maximumPopulation(int[][] logs) {
int[] d = new int[101];
final int offset = 1950;
for (var log : logs) {
int a = log[0] - offset;
int b = log[1] - offset;
++d[a];
--d[b];
}
int s = 0, mx = 0;
int j = 0;
for (int i = 0; i < d.length; ++i) {
s += d[i];
if (mx < s) {
mx = s;
j = i;
}
}
return j + offset;
}
}
// Accepted solution for LeetCode #1854: Maximum Population Year
func maximumPopulation(logs [][]int) int {
d := [101]int{}
offset := 1950
for _, log := range logs {
a, b := log[0]-offset, log[1]-offset
d[a]++
d[b]--
}
var s, mx, j int
for i, x := range d {
s += x
if mx < s {
mx = s
j = i
}
}
return j + offset
}
# Accepted solution for LeetCode #1854: Maximum Population Year
class Solution:
def maximumPopulation(self, logs: List[List[int]]) -> int:
d = [0] * 101
offset = 1950
for a, b in logs:
a, b = a - offset, b - offset
d[a] += 1
d[b] -= 1
s = mx = j = 0
for i, x in enumerate(d):
s += x
if mx < s:
mx, j = s, i
return j + offset
// Accepted solution for LeetCode #1854: Maximum Population Year
struct Solution;
impl Solution {
fn maximum_population(logs: Vec<Vec<i32>>) -> i32 {
let mut start = std::i32::MAX;
let mut end = 0;
for log in &logs {
let birth = log[0];
let death = log[1];
start = start.min(birth);
end = end.max(death);
}
let mut res = 0;
let mut max = 0;
for i in start..end {
let mut population = 0;
for log in &logs {
let birth = log[0];
let death = log[1];
if (birth..death).contains(&i) {
population += 1;
}
}
if max < population {
res = i;
max = population;
}
}
res
}
}
#[test]
fn test() {
let logs = vec_vec_i32![[1993, 1999], [2000, 2010]];
let res = 1993;
assert_eq!(Solution::maximum_population(logs), res);
let logs = vec_vec_i32![[1950, 1961], [1960, 1971], [1970, 1981]];
let res = 1960;
assert_eq!(Solution::maximum_population(logs), res);
}
// Accepted solution for LeetCode #1854: Maximum Population Year
function maximumPopulation(logs: number[][]): number {
const d: number[] = new Array(101).fill(0);
const offset = 1950;
for (const [birth, death] of logs) {
d[birth - offset]++;
d[death - offset]--;
}
let j = 0;
for (let i = 0, s = 0, mx = 0; i < d.length; ++i) {
s += d[i];
if (mx < s) {
mx = s;
j = i;
}
}
return j + offset;
}
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.