PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep

Quick Overview

This question evaluates proficiency in numerical simulation, portfolio optimization, time-series data processing, and computation of risk-return metrics such as expected return, volatility, and the Sharpe ratio.

  • Medium
  • DRW
  • Coding & Algorithms
  • Machine Learning Engineer

Implement portfolio optimization simulation

Company: DRW

Role: Machine Learning Engineer

Category: Coding & Algorithms

Difficulty: Medium

Interview Round: Take-home Project

##### Question Given price-return time series in a DataFrame, simulate random portfolio weights, compute expected return, volatility, Sharpe ratio, and return the weight vector that maximizes the Sharpe ratio.

Quick Answer: This question evaluates proficiency in numerical simulation, portfolio optimization, time-series data processing, and computation of risk-return metrics such as expected return, volatility, and the Sharpe ratio.

You are building the core selection step of a portfolio optimization simulation (the kind a quant runs by sampling thousands of random portfolios). You are given the per-asset statistics derived from a price-return time series, plus a finite set of candidate portfolio weight vectors (these stand in for the randomly simulated portfolios). For each candidate you must compute its Sharpe ratio and return the single weight vector that maximizes it. For a weight vector w over n assets: - Portfolio expected return: R = sum_i( w[i] * mean_returns[i] ) - Portfolio variance: V = sum_i sum_j( w[i] * cov[i][j] * w[j] ) (the quadratic form w^T * cov * w) - Portfolio volatility: sigma = sqrt(V) - Sharpe ratio: S = (R - risk_free) / sigma Return the candidate weight vector with the highest Sharpe ratio. Inputs: - mean_returns: list of n floats, the expected return of each asset. - cov: an n x n covariance matrix (list of lists of floats), symmetric and positive semidefinite. - risk_free: a float, the risk-free rate to subtract from portfolio return. - candidates: a list of weight vectors; each is a list of n floats. Rules: - If a candidate has zero (or negative) variance, its Sharpe ratio is undefined; skip it. - If two candidates are tied, return the first one encountered (lowest index). - If no candidate has positive variance, return None / null. Return the winning weight vector exactly as it was passed in.

Constraints

  • 1 <= n (number of assets) <= 50
  • cov is an n x n symmetric, positive semidefinite matrix
  • Each candidate weight vector has length n
  • 0 <= number of candidates <= 10000
  • Weights, returns, covariances, and risk_free are real numbers (may be negative; weights need not be normalized)
  • Skip any candidate whose portfolio variance is <= 0 (Sharpe undefined)
  • Return the first (lowest-index) candidate on a tie; return None if no candidate has positive variance

Examples

Input: ([0.1, 0.2], [[0.04, 0.0], [0.0, 0.09]], 0.02, [[1.0, 0.0], [0.0, 1.0], [0.5, 0.5]])

Expected Output: [0.5, 0.5]

Explanation: All-A: return 0.1, vol 0.2, Sharpe (0.1-0.02)/0.2 = 0.40. All-B: return 0.2, vol 0.3, Sharpe (0.2-0.02)/0.3 = 0.60. 50/50: return 0.15, variance 0.25*0.04 + 0.25*0.09 = 0.0325, vol 0.1803, Sharpe (0.15-0.02)/0.1803 = 0.721. The diversified 50/50 portfolio has the highest Sharpe.

Input: ([0.05], [[0.01]], 0.0, [[1.0]])

Expected Output: [1.0]

Explanation: Single asset, single candidate: variance = 1*0.01*1 = 0.01, vol 0.1, Sharpe (0.05-0)/0.1 = 0.5. It is the only valid candidate, so it is returned.

Input: ([0.1, 0.1], [[0.04, 0.0], [0.0, 0.04]], 0.0, [[1.0, 0.0], [0.5, 0.5]])

Expected Output: [0.5, 0.5]

Explanation: Both assets have the same return (0.1) and same variance (0.04). All-A: vol 0.2, Sharpe 0.5. 50/50: variance 0.25*0.04 + 0.25*0.04 = 0.02, vol 0.1414, Sharpe 0.1/0.1414 = 0.707. Diversifying across uncorrelated equal assets cuts volatility and raises the Sharpe.

Input: ([0.1, 0.2, 0.15], [[0.04, 0.01, 0.0], [0.01, 0.09, 0.02], [0.0, 0.02, 0.0625]], 0.03, [[0.4, 0.3, 0.3], [0.6, 0.2, 0.2], [0.2, 0.5, 0.3]])

Expected Output: [0.4, 0.3, 0.3]

Explanation: Three correlated assets, three candidate weightings. Computing each Sharpe via the full quadratic form (including the off-diagonal covariance terms), the [0.4, 0.3, 0.3] allocation yields the highest risk-adjusted return.

Input: ([0.1, 0.2], [[0.04, 0.0], [0.0, 0.09]], 0.02, [[0.0, 0.0]])

Expected Output: None

Explanation: The only candidate is the all-zero weight vector, whose portfolio variance is 0. Its Sharpe ratio is undefined, so it is skipped, and with no valid candidate remaining the function returns None.

Hints

  1. Portfolio variance is the quadratic form w^T * cov * w: a double loop sum over i and j of w[i] * cov[i][j] * w[j]. Don't forget the off-diagonal cross terms.
  2. Sharpe ratio = (portfolio_return - risk_free) / sqrt(variance). Guard against zero/negative variance before taking the square root, and skip those candidates.
  3. Track the best Sharpe seen so far and only replace it on a strictly-greater value, so the earliest candidate wins ties.
Last updated: Jun 25, 2026

Loading coding console...

PracHub

Master your tech interviews with 8,000+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.

Related Coding Questions

  • Solve three algorithmic OA problems - DRW (medium)
  • Compute rolling standard deviation in O(n) - DRW (Medium)
  • Solve movie ratings, array, release scheduler - DRW (Medium)
  • Solve odd-string, digit swap, patient slot assignment - DRW (Medium)
  • Solve three algorithmic OA tasks - DRW (Medium)