Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
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 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
Build my plan
Largest Drawdown Detection in Price Series
00:00
5 left

Largest Drawdown Detection in Price Series

HardPython

Problem

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]].

Constraints

  • 1 <= prices.length <= 10000
  • 1 <= n <= 10000
  • prices contains positive real numbers
  • A drawdown is measured as (peak_price - trough_price) / peak_price
  • Drawdown episodes are non-overlapping and end at the first recovery of their peak

Function Signature

def largest_drawdowns(prices, n):
Interviewer

Your question is Largest Drawdown Detection in Price Series. 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.