Money-Market Yield
Counting the actual days between two dates is the easy part the model gets for free. The hidden test is what you divide those days by to turn a rate into a fraction of a year: a vague spec assumes a calendar year, which quietly disagrees with the convention this kind of short-term instrument is quoted under for every non-empty period.
You are building the interest calculator for a short-term cash instrument: a deposit, a bill, a money-market placement. It is handed a principal, an annual interest rate expressed as a decimal (so 0.05 means five percent per year), and two calendar dates: the date interest starts accruing and the date it stops. It returns, as a single number, the interest earned on the principal over the period from the start date up to but not including the end date. Equal dates earn nothing.
Implement a function interest(principal, annualRate, startISO, endISO). The two dates arrive as 'YYYY-MM-DD' strings and the start date is never after the end date. The interest is the principal times the annual rate times the fraction of a year the period covers. The day count itself is the literal number of calendar days between the two dates; the arithmetic is straightforward, and the entire result hinges on a single modeling choice you must make and pin down precisely.
Specify how the calculator turns a count of days into a fraction of a year. Dividing the actual day count by 365 is the obvious choice, but it is not the one this kind of short-term instrument is conventionally quoted under. There is a standard, widely used convention for exactly this family of instruments that counts the real days elapsed but divides by a year length that is not 365. Decide which convention governs, state the year length it divides by exactly, and write it as something a deterministic test could check. A calculator that divides by a calendar year will quietly disagree with the convention for every period that actually earns interest.
With principal 1,000,000 and annualRate 0.06, earning from 2024-01-01 to 2024-03-31 spans 90 actual days. Divide those 90 days by a calendar year and you get one number; divide by the year length this instrument is quoted under and you get another, larger one. The two answers diverge for any period that is not empty.
- interest(principal, annualRate, startISO, endISO) returning a number; equal dates return 0.
- Interest is principal times annualRate times the fraction of a year the period covers.
- Dates are 'YYYY-MM-DD' strings with start no later than end; the rate is a decimal.
The functional tests are shown, and the model usually clears them on its own. The hidden tests are the twists this kind of system is full of. They are not listed. Your spec only passes them if it already knows where this domain breaks.
254 chars