Choose the Earliest Year of Peak Population

Quick Overview

Choose the Earliest Year of Peak Population. Each pair in logs gives one person’s birth and death years. Count a person from the birth year through the year before death. Return the earliest year with the greatest population.

Choose the Earliest Year of Peak Population

Company: Google

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

### Problem Each pair in `logs` gives one person’s birth and death years. Count a person from the birth year through the year before death. Return the earliest year with the greatest population. ```cpp class Solution { public: int maximumPopulation(vector<vector<int>>& logs); }; ``` ### Examples **Example 1:** ```text logs = [[2001,2005],[2003,2007],[2005,2008]] Output: 2003 Explanation: Population first reaches two in 2003; the birth and death at 2005 cancel. ``` **Example 2:** ```text logs = [[1962,1964],[1970,1972]] Output: 1962 Explanation: The greatest population is one, first reached in 1962. ``` ### Constraints - `1 <= logs.length <= 100`. - Every pair satisfies `1950 <= birth < death <= 2050`. ### Clarifying Questions Birth is inclusive and death exclusive. Return the earliest maximum, not the most recent. ```hint Inspect the structure How do birth and death years contribute at their endpoints, and how is a tied maximum resolved? ``` ### Reasoning checkpoints - Apply birth-inclusive and death-exclusive population semantics. - Show that the returned year attains the largest population and is the earliest such year. ### Optional extensions How would a sorted event sweep handle a much larger year domain?

Overview: Choose the Earliest Year of Peak Population. Each pair in logs gives one person’s birth and death years. Count a person from the birth year through the year before death. Return the earliest year with the greatest population.

Google logo
Google
May 30, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

Problem

Each pair in logs gives one person’s birth and death years. Count a person from the birth year through the year before death. Return the earliest year with the greatest population.

class Solution {
public:
    int maximumPopulation(vector<vector<int>>& logs);
};

Examples

Example 1:

logs = [[2001,2005],[2003,2007],[2005,2008]]
Output: 2003
Explanation: Population first reaches two in 2003; the birth and death at 2005 cancel.

Example 2:

logs = [[1962,1964],[1970,1972]]
Output: 1962
Explanation: The greatest population is one, first reached in 1962.

Constraints

  • 1 <= logs.length <= 100 .
  • Every pair satisfies 1950 <= birth < death <= 2050 .

Clarifying Questions Guidance

Birth is inclusive and death exclusive. Return the earliest maximum, not the most recent.

Reasoning checkpoints

  • Apply birth-inclusive and death-exclusive population semantics.
  • Show that the returned year attains the largest population and is the earliest such year.

Optional extensions

How would a sorted event sweep handle a much larger year domain?