This is the Bonus Round problem for the 2025 Cody Contest. Attend the watch party on Friday, March 27 to see how 6 champions from the three contest teams solved this problem. For more information, see Chen's announcement.
In a rare moment of wisdom, Lord Ned realized that he should reward his advisors for their years of service to Nedland. It also occurred to him that it might be insightful to determine the distribution of his advisors' tenures, to see if he is being counseled by a variety of perspectives. Unfortunately, he then reverted to (slightly insane) form and got overly carried away with the idea, decreeing that all his government departments must report the annual distribution of their staff tenures, going back the last 13 years. He wants the distribution binned into 5-year groups. That is, 0-4 years, 5-9 years, 10-14 years, and so on, up to the longest-serving official on staff for that department.
Having some familiarity with Lord Ned's eccentricities, his Minister of Egregious Summaries and Statistics realized that this reporting should be simple to automate, by someone with a little programming experience.
Given an n-by-2 matrix representing the start and end years of each staff member, return a 13-by-m matrix giving the number of staff members in each of the 5-year bins for each of the 13 years from 2013 to 2025.
The first column of the input gives the year each person started working for the department. The second column gives the year they left. If they are still working, the value in the second column will be NaN.
For the output matrix A, A(j,k) is the number of people in the jth year that have a tenure in the kth bin. The years are 2013 to 2025. The tenure bins are 0-4, 5-9, 10-14, etc. Therefore, A(j,k) is the number of people in year (2012+k) that have been employed between 5*(k-1) and (5*k - 1) years. A will always have 13 rows. The number of columns of A (m) will depend on the longest tenure of the department's staff members. This will depend on initial hire dates, some of which might be significantly before 2013.
Lord Ned has decreed that he wants an end-of-year report for each year. Therefore if someone leaves in the current year, they are not included in the count. Anyone who has joined in that year is counted, with a tenure rounded down to 0 years. Tenures continue to be rounded down, so if someone joined in 2010, in 2014 they are counted as a 4-year tenure.
These rules mean that, in the example image, the counts can be determined by looking at the colored bars immediately to the right of the dashed line that represents the year.
Example
yrs = [1999 2013;
2002 2022;
2005 2016;
2010 2016;
2013 NaN;
2016 2024;
2016 2023;
2022 NaN;
2023 NaN;
2024 NaN];
In 2013, there were four staff members. The longest serving staff member (14 years) left in this year and was replaced by someone new. Therefore for 2013, the tenures recorded are 11, 8, 3, 0, which means 2 people in 0-4 years, 1 in 5-9 years, 1 in 10-14.
In 2014, the tenures recorded are 12, 9, 4, 1, which gives the same distribution.
In 2015, the tenures are 13, 10, 5, 2, which means the distribution shifts to [1 1 2].
In 2016, two people leave and are replaced. Tenures of 14, 3, 0, 0 result in a distribution of [3 0 1].
In 2017, the tenures are 15, 4, 1, 1, which means the distribution is now [3 0 0 1] (for the first time, we have someone in the 15-19 year group).
In 2022, the staff member that started in 2002 left. This would have been their 20-year anniversary, but because they left, they are not counted in the 2022 report. No other staff member reaches a 20-year tenure, so the output matrix has four columns (0-4, 5-9, 10-14, 15-19).
A = fiveyearcounts(yrs)
A =
2 1 1 0
2 1 1 0
1 1 2 0
3 0 1 0
3 0 0 1
2 1 0 1
2 1 0 1
2 1 0 1
0 3 0 1
1 3 0 0
2 1 1 0
3 0 1 0
3 0 1 0
Assumptions
The report needs to work only for years 2013 to 2025. However, staff members may be hired any time before 2013, which means the output matrix can have any number of columns. The input matrix will not include staff members who left before 2013.
Solution Stats
Problem Comments
1 Comment
Solution Comments
Show comments
Loading...
Problem Recent Solvers3
Suggested Problems
-
3 Solvers
More from this Author35
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
Nice one, Matt! It seems you can always count on good old Lord Ned for entertaining Cody problems.