Portfolio Risk Engine
Hard ● Live C++ Rust Java PythonThe Problem
Analyze a portfolio of N assets with D days of historical returns. Compute comprehensive risk metrics: portfolio volatility, Sharpe ratio, Value-at-Risk (VaR), Conditional Value-at-Risk (CVaR), maximum drawdown, and asset correlation matrix.
Scale: 50 assets, 2520 days (~10 years of daily data = 500K data points). Handle large financial datasets efficiently while maintaining numerical accuracy.
Evaluation Criteria
Correctness (70%)
All risk metrics computed correctly. Tolerance: 0.001 for VaR/CVaR/volatility/Sharpe/drawdown; 0.01 for correlations.
Performance (30%)
Wall-clock time on the 500K data point dataset. Memory usage must not exceed 256 MB.
Input Format
- First line
N Dwhere N = number of assets, D = number of trading days.- Second line
- N weights (sum to 1) - the current portfolio weighting.
- Next D lines
- each is N returns (decimal, e.g.,
0.0040= 0.4%). - Production scale
- N=50, D=2520 (~10 years).
Output Format & Sample
Sample input (first 8 lines, full file linked below):
5 20 0.1530295311 0.2860889326 0.0638790800 0.1438406222 0.3531618342 -0.0100366910 -0.0250240866 -0.0109378051 0.0117757652 -0.0079889820 -0.0134936596 0.0180259052 0.0099497196 -0.0042169790 -0.0016099298 0.0311712229 -0.0117215857 0.0058807138 0.0100631574 0.0131721201 0.0090259594 -0.0197471563 0.0039064539 -0.0065307798 0.0047461850 -0.0243164261 0.0023513982 -0.0095027779 -0.0109069766 -0.0149782567 0.0059657482 -0.0031979695 0.0085983319 -0.0054953968 0.0044407148 ... (more)
Expected output for that sample (first 8 lines):
0.136151 -3.911555 0.014154 0.015918 0.015036 0.015918 0.052450 1.000000 0.054903 0.319107 0.271337 0.754767 ... (more)
↓ download full sample input | ↓ download expected output
Test your solution locally:
./your_solution < input.txt | diff - output.txt
Common Pitfalls
- Output exactly 7 scalars first, then the upper triangular correlation matrix. Order: portfolio volatility (annualized), Sharpe ratio (assume rf=0), VaR(95%), CVaR(95%), VaR(99%), CVaR(99%), max drawdown.
- Annualize volatility by sqrt(252). The grader expects daily vol times sqrt(252), not raw daily vol.
- VaR is positive. 95% VaR is the absolute value of the 5th percentile of portfolio returns. Don't report a negative number.
- CVaR is the average of the tail beyond VaR, not the VaR value itself.
- Correlation matrix is upper triangular, row-major, space-separated. Include the diagonal (which is all 1.0). Total entries = N*(N+1)/2.
- Use NumPy. Naive O(N*D) Python loops for the covariance computation will not hit Gold.
Performance Tier Thresholds (applied by the grader)
| Language | Gold | Silver | Bronze | Tier 4 |
|---|---|---|---|---|
| C++ | <500ms | <2000ms | <10000ms | else PASS |
| Rust | <500ms | <2000ms | <10000ms | else PASS |
| Java | <1000ms | <4000ms | <15000ms | else PASS |
| Python | <5000ms | <15000ms | <60000ms | else PASS |
Submission Rules
- Single-file solutions only. All code must be in one file.
- Your program must read from stdin and write to stdout.
- Java class must be named Solution.
- No network access, no filesystem access beyond stdin/stdout.
- Time limit: 120 seconds. Memory limit: 256 MB.