Dataford
Interview QuestionsInterview GuidesExperiencesCoaching
New
Pricing
Get started
Dataford
Popular roles
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Browse
Browse All RolesEvery role hub, from analyst to MLBrowse All CompaniesCompany-specific interview loopsAll Interview GuidesThe full guide library
Top questions by role
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Top questions by skill
SQLPythonStatisticsMachine LearningA/B TestingSystem DesignGenerative AIProduct SenseMetricsBehavioral
Browse all questions →Try a mock interview
Experiences
Practice
Mock InterviewsTimed interview simulations with feedbackCoaching1:1 coaching with people at your target companiesSuccess PathYour 6-week structured planModulesCurated lessons by topicWebinarsTalks from ex-Big Tech data leadsPlaygroundA free-form scratch editor
Learn
BlogInterview strategy and career adviceTech Job Market ReportHiring trends across data and AI rolesFor UniversitiesDataford for career centersAbout DatafordWho we are and how we build
Pricing
Build my plan
Programmatic Compound Interest
00:00
5 left

Programmatic Compound Interest

EasyPython

Problem

An RE Partners financial planning workflow needs to estimate the future balance of an investment. Given an initial principal, an annual interest rate, a compounding frequency, and a duration, calculate the final balance using compound interest.

Use the formula A = P * (1 + r / n) ** (n * t), where P is the principal, r is the annual interest rate as a decimal, n is the number of compounding periods per year, and t is the number of years. Return the balance rounded to two decimal places.

Formal Specification

Implement calculate_compound_interest(principal, annual_rate, compounds_per_year, years).

  • principal is a nonnegative number representing the initial balance.
  • annual_rate is a nonnegative percentage, such as 5 for 5% annual interest.
  • compounds_per_year is a positive integer.
  • years is a nonnegative integer.
  • Return a floating-point number representing the final balance, rounded to two decimal places.

Constraints

  • 0 <= principal <= 10^9
  • 0 <= annual_rate <= 100
  • 1 <= compounds_per_year <= 365
  • 0 <= years <= 100
  • Return the result rounded to two decimal places

Function Signature

def calculate_compound_interest(principal, annual_rate, compounds_per_year, years):
Interviewer

Your question is Programmatic Compound Interest. Start with the requirements in the Question tab.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.
CodePython 3
Sign up free to run your codeLog inLn 2
Run your code to see test output here.