FastPrepPopulation Alive in a Target Year

Population Alive in a Target Year

Bloomberg LP logoBloomberg LP● EasyNEW GRADONSITE INTERVIEW
Learn

Problem statement

The parallel arrays birthYears and deathYears describe a population. Person i is alive in every integer year from birthYears[i] through deathYears[i], inclusive.

Given an integer year, return how many people are alive during that year. Return 0 when the population is empty.

Function

countPopulationInYear(birthYears: int[], deathYears: int[], year: int) → int

Examples

Example 1

birthYears = [1900,1920,1930]deathYears = [1950,1940,1970]year = 1935return = 3

All three people were born by 1935 and none had died before 1935.

Example 2

birthYears = [2000,1990,2010]deathYears = [2000,1999,2015]year = 2000return = 1

The first person counts in both the birth and death year. The second died before 2000, and the third was born later.

Example 3

birthYears = []deathYears = []year = 2025return = 0

An empty population has nobody alive in any year.

Constraints

  • 0 <= birthYears.length = deathYears.length <= 100000.
  • 0 <= birthYears[i] <= deathYears[i] <= 1000000000.
  • 0 <= year <= 1000000000.

More Bloomberg LP problems

See Bloomberg LP hiring insights
public int countPopulationInYear(int[] birthYears, int[] deathYears, int year) {
    // Write your code here.
}
birthYears[1900,1920,1930]
deathYears[1950,1940,1970]
year1935
expected3
Checking account…