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
Implement Value at Risk (VaR)
00:00
5 left

Implement Value at Risk (VaR)

HardPython

Problem

In an EY Oman Quantitative Advisory analysis, calculate the historical Value at Risk for a portfolio of assets. Use each asset's historical daily price changes to create portfolio profit-and-loss scenarios, then return the loss percentile at the requested confidence level.

For each asset, apply its historical percentage return to its current market value, where current market value is quantity * latest_price. Sum the resulting P&L across all assets for each historical day. Portfolio loss is the negative of portfolio P&L.

Use the nearest-rank percentile rule: sort the losses and select the element at zero-based index ceil(confidence * number_of_scenarios) - 1. Return the VaR as a numeric value in portfolio currency. A negative result is valid when the selected percentile represents a gain.

Formal Specification

Implement calculate_var(positions, confidence). positions is a list of dictionaries, each containing quantity and prices. Every prices list contains at least two positive daily prices and all assets have the same number of observations. confidence is a decimal strictly between 0 and 1. Return a floating-point VaR.

Constraints

  • 1 <= len(positions) <= 10^4
  • 2 <= len(prices) <= 10^4
  • All assets have equal price-history lengths
  • prices[i] > 0
  • quantity may be positive or negative
  • 0 < confidence < 1

Function Signature

def calculate_var(positions, confidence):
Interviewer

Your question is Implement Value at Risk (VaR). 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.