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
Experiences
Practice
Mock InterviewsTimed interview simulations with feedbackCoaching1:1 coaching with people at your target companiesSuccess 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
Writing a Machine Learning Function
00:00
5 left

Writing a Machine Learning Function

HardPython

Problem

St. Jude Children's Research Hospital genomic analysis pipelines can model an observed sequence as emissions from hidden biological states. Implement the Viterbi algorithm to return the most likely sequence of hidden state IDs for a Hidden Markov Model.

Formal Specification

Implement viterbi_decode(start_probs, transition_probs, emission_probs, observations).

  • start_probs[i] is the probability that state i starts the sequence.
  • transition_probs[i][j] is the probability of moving from state i to state j.
  • emission_probs[i][k] is the probability that state i emits observation k.
  • observations contains integer observation IDs.

Return a list of state IDs, one per observation, with maximum total probability. If multiple paths have the same probability, return the path selected by choosing the smallest predecessor state ID at each tie.

Use log probabilities internally. A probability of 0 represents an impossible transition or emission and must not cause a math-domain error.

Constraints

  • 1 <= len(start_probs) <= 200
  • len(transition_probs) == len(emission_probs) == len(start_probs)
  • transition_probs is a square state-by-state matrix
  • 0 <= len(observations) <= 2,000
  • 0.0 <= every probability <= 1.0
  • Each observation ID is a valid index in every emission probability row
  • At least one valid state path exists for every non-empty observation sequence

Function Signature

def viterbi_decode(start_probs, transition_probs, emission_probs, observations):
Interviewer

Your question is Writing a Machine Learning Function. 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.