Last Sunday
Level: Beginner 15–30 minConcepts: Algorithms
Solutions: C# | TypeScript | Python
Write a method that returns the last Sunday of each month for a given year.
Example
last_sunday(2013)
| Result |
|---|
| 2013-01-27 |
| 2013-02-24 |
| 2013-03-31 |
| 2013-04-28 |
| 2013-05-26 |
| 2013-06-30 |
| 2013-07-28 |
| 2013-08-25 |
| 2013-09-29 |
| 2013-10-27 |
| 2013-11-24 |
| 2013-12-29 |
Hint
If you input the year 2020, a leap year, is the last Sunday of February 2020-02-23? If you input the year 2032, also a leap year, is the last Sunday February 2032-02-29?
Reference Walkthrough
Reference implementations in C#, TypeScript, and Python live at tddbuddy-reference-katas/last-sunday. The reference exposes the per-month primitive — find(year, month) with month 1-indexed per human convention — because the per-year list from the TDD Buddy prompt is a trivial map over 1..12 once the month-level math is solved. Ten scenarios cover January through December of 2013, leap-year February edges (2020-02-23, 2032-02-29 where the leap day is Sunday), a century non-leap (2100-02-28), and a four-hundred-divisible leap (2000-12-31 where the last day of the year is Sunday). Each implementation constructs the last day of the month from the standard library and walks back zero-to-six days to Sunday using (weekday_delta + 7) % 7, so the zero-step case falls out of the same arithmetic.
- C# (.NET 8, xUnit, FluentAssertions 6.12.0) — walkthrough —
public static DateOnly Find(int year, int month) - TypeScript (Node 20, Vitest 1.6, TS 5 strict) — walkthrough — UTC-disciplined
Date: constructed viaDate.UTC(...), read viagetUTCDay()/getUTCDate()so calendar components stay stable across host time zones - Python (3.11, pytest) — walkthrough —
datetime.date; note Python’sweekday()has Sunday=6 where C# / JavaScript have Sunday=0, so the walk-back arithmetic changes its target integer but keeps the same shape
This kata ships in Agent Full-Bake mode at high gear (F1 tier): the algorithm is small enough to land as one commit per language, with a brief walkthrough noting there are no builders because the date type comes from the standard library and the inputs/outputs are the domain. See the repo’s Gears section for when high gear is the right call.
Solutions
You can find all language solutions here Last Sunday Solutions
Or you can select a specific language below.
Related reading
- Katas Are Rehearsal, Not Performance
You don't practice TDD on production code. You practice on katas and bring the muscle memory to production. The gap between knowing TDD and doing TDD is reps. - The Test Pyramid Was an Economic Argument
The test pyramid was not a quality law. It was a cost structure: unit tests were cheap, integration tests were expensive, so you wrote many of the first and few of the second. Agents collapsed the cost of writing tests at every level, and the cheapest test that still tells the truth is the one that pins a seam the agent cannot fake. - Agents Pattern-Match Your Test Smells
Test smells used to drift in at typing speed. Agents reproduce them at generation speed because they sample whatever is already in the suite. The smells in your codebase are now the smells in your roadmap.