Given a price/return series, write code that returns the n largest drawdowns.
Asked in the Online assessment stage. This was the programming part of the OA, and the candidate called it hard.
Implement largest_drawdowns(prices, n). A drawdown starts when prices fall below a running high, reaches its lowest trough, and ends when the prior high is recovered. Return up to n records as [peak_index, trough_index, recovery_index, drawdown_fraction], sorted from largest to smallest. Use -1 for an unrecovered drawdown. Exclude zero drawdowns. For ties, sort by earlier peak index.
Example: prices = [100, 95, 90, 96, 101, 98, 80, 102], n = 2 returns [[4, 6, 7, 0.2079207921], [0, 2, 4, 0.1]].
def largest_drawdowns(prices, n):