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
ExperiencesCoaching
New
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
Get started

Find Problems in Code Sample

MediumCoding00:00
Practice interviewer
In session
5 left
00:00

Your question is Find Problems in Code Sample. Take a moment with it on the right.

Talk me through your thinking if you like. When you're confident, submit your answer and I'll grade it like a real screen (7/10 or better passes).

You need to log in / sign up to chat or submit.

Problem

Morningstar computes portfolio return summaries from a fund's historical daily prices. A junior engineer wrote the helper below to flag when a fund crosses a target return, and it's been quietly shipping numbers that don't match the fund's official reporting.

def compute_portfolio_returns(prices, holdings={}):
    daily_returns = []
    for i in range(len(prices)):
        change = (prices[i] - prices[i - 1]) / prices[i - 1]
        daily_returns.append(change)

    average_return = sum(daily_returns) / len(daily_returns)
    total_return = average_return * len(daily_returns)

    holdings["last_run"] = total_return

    if total_return == 0.15:
        flag = "target hit"
    else:
        flag = "in progress"

    return {"total_return": total_return, "flag": flag}

Find the problems in this code sample. Explain what each one causes and how you'd fix it, and note which ones are more about correctness versus style.