Calculating Mortgage Interest Rates
Beginner Mode

Start your terminal to use beginner mode.

Objective

You are working for a mortgage company which holds records of multiple mortgage types from various users. The company maintains two different DataFrames: MortgageDetails (which logs all the mortgage types and their base rates) and UserMortgages (which tracks which user selected which mortgage).

Task

Write a PySpark function that calculates the interest rate for each type of mortgage based on user adoption.

The RateOfMortgage is calculated specifically as the sum of the InterestRate for each MortgageType divided by the count of UserID for each MortgageType.

Save your resulting DataFrame as result_df. Order the final output alphabetically by MortgageType.

File Path

  • Details Dataset: /home/interview/mortgage_details.csv
  • Users Dataset: /home/interview/user_mortgages.csv
  • Starter script: /home/interview/mortgage_rates.py

Schema

mortgage_details.csv

Column Name Type
MortgageID String
MortgageType String
InterestRate Double

user_mortgages.csv

Column Name Type
UserID String
MortgageID String

Expected Output Schema

Column Name Type
MortgageType String
RateOfMortgage Double

Example

Given this sample input:

MortgageDetails

MortgageID MortgageType InterestRate
M1 Fixed 4.5
M2 Variable 3.2
M3 Adjustable 2.8

UserMortgages

UserID MortgageID
U1 M1
U2 M1
U3 M2
U4 M3

The expected output would be:

MortgageType RateOfMortgage
Adjustable 2.8
Fixed 4.5
Variable 3.2

Explanation: * "Fixed" has two users (U1, U2). The sum of their interest rates is 4.5 + 4.5 = 9.0. Divided by 2 users = 4.5.

  • "Variable" has one user (U3). Sum is 3.2. Divided by 1 user = 3.2.

  • "Adjustable" has one user (U4). Sum is 2.8. Divided by 1 user = 2.8.

Terminal requires a larger screen

Open this page on a desktop or tablet (≥ 768px) to launch the terminal and practice hands-on.

Linux Terminal Environment

Write and execute your solution in the terminal below.

Sign In

Track

Question Difficulty Company Access
Need more practice in this area? Explore more questions →