Question presented to candidate: "Build a mortgage calculator. I enter the loan amount, the yearly interest rate and the term in years, and it shows my monthly payment, the total I will pay and the total interest. Results should update as I type. Make sure the numbers are actually right."
Requirements:
- Inputs: loan amount, annual interest rate (%), and term (15, 20 or 30 years).
- Outputs: monthly payment, total of all payments, total interest, and the first month's split between interest and principal.
- Results update live; invalid input (empty amount, a rate outside 0-100) shows a message next to the field and blanks the results.
- Correct maths: the fixed-rate amortisation formula, the 0% special case, and money handled to the cent.
- Currency formatting, labelled inputs, readable at phone width.
What a strong answer should cover:
- The formula
M = P ยท r(1 + r)^n / ((1 + r)^n โ 1)withr= annual rate / 12 / 100 andn= years ร 12, and whyr = 0needsP / n. - Results as derived values (no "calculate" state, no effect).
- Rounding: the payment is paid in whole cents, so
monthly ร nis not the true total; a lender adjusts the last payment. - Validation as pure functions returning messages, plus
aria-invalid/aria-describedby. Intl.NumberFormatfor currency instead of hand-built strings.
Clarifying questions expected:
- "Fixed rate only, or also adjustable rates, taxes and insurance?"
- "Do you want the full amortisation schedule or just the summary?"
- "Which currency and locale?"
Code / implementation expected: Yes. A component plus a pure calculation module, roughly 80-120 lines.