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
ExperiencesCoaching
New
Practice
Mock InterviewsTimed interview simulations with feedbackSuccess 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
Get started
Portfolio Valuation From Python Data
00:00
5 left

Portfolio Valuation From Python Data

HardSQL · PostgreSQL

Problem

Moody's gave you a Python script that is supposed to value a bond-and-equity portfolio using boilerplate holdings, price, and FX data, and asked you to review it before it gets used for a client report.

holdings = [
    {'symbol': 'AAPL', 'quantity': 500, 'currency': 'USD', 'asset_type': 'equity'},
    {'symbol': 'GOOGL', 'quantity': -100, 'currency': 'USD', 'asset_type': 'equity'},
    {'symbol': 'BUND10Y', 'quantity': 2000, 'currency': 'EUR', 'asset_type': 'bond'},
]

prices = {'AAPL': 190.20, 'GOOGL': 141.80, 'BUND10Y': 98.50}
fx_rates = {'USD': 1.0, 'EUR': 1.09}

def calculate_portfolio_value(holdings, prices, fx_rates=None):
    if fx_rates is None:
        fx_rates = {}
    total = 0
    for h in holdings:
        qty = abs(h['quantity'])
        price = prices.get(h['symbol'], 0)
        total += qty * price
    return total

print(calculate_portfolio_value(holdings, prices, fx_rates))

Explain, in your own words, everything wrong with how this script computes portfolio value and what you would change so the printed number is actually correct.

Write a SQL query that produces the corrected valuation for every portfolio in the supplied data. Use signed quantities, convert each valued position to USD, and expose positions that cannot be valued because a price or FX rate is missing.

Output

  1. One row per portfolio with portfolio_id, portfolio_value_usd, valued_positions, and missing_valuation_inputs.
  2. Positions with a NULL price or NULL FX rate are ignored in the value and counted as missing.
  3. Sort by portfolio_id ascending.

Schema

portfolio_holdings
ColumnTypeDescription
holding_idPKINTUnique holding identifier
portfolio_idVARCHAR(30)Portfolio identifier
symbolVARCHAR(20)Security symbol
quantityNUMERIC(18,4)Signed position quantity
currencyVARCHAR(10)Position currency
asset_typeVARCHAR(30)Asset classification
market_prices
ColumnTypeDescription
symbolPKVARCHAR(20)Security symbol
priceNUMERIC(18,4)Latest local-currency price
fx_rates
ColumnTypeDescription
currencyPKVARCHAR(10)Currency code
usd_rateNUMERIC(18,8)USD value of one unit of currency
Tablesportfolio_holdingsmarket_pricesfx_rates
Interviewer

Whenever you're ready, walk me through your approach or go straight to the code. The requirements and the three tables are in the Question tab.

You need to log in / sign up to run or submit.
CodePostgreSQL
Sign up free to run your codeLog inLn 1
Run your query to see results here.