Compute max population given a list of people's birth and death year
Your approach isn't bad if you want to do multiple analyses of the population over time. For programming puzzles I often find it useful to focus in on exactly what's being requested and leave the other interesting stuff for another day.

persons.flatMap(p =&gt; Seq((p.birthYear, 1), (p.deathYear, -1)))
       .sorted
       .foldLeft((0,0,0)){case ((mxYear,mxPop,curPop), (thisYear,dif)) =&gt;
         if (curPop+dif &gt; mxPop) (thisYear, curPop+dif, curPop+dif)
         else (mxYear, mxPop, curPop+dif)
       }._1  //res0: Int = 2000


In this case the data is


traversed once - to attach a 1 or -1
sorted - to get all the years in order
traversed a 2nd and final time - to count the population changes and save the maxes