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
Probability of Coin Heads
00:00
5 left

Probability of Coin Heads

MediumPython

Problem

StackAdapt experiments may model a fixed number of successful outcomes across a small sequence of independent trials. Given n coin positions, exactly k of which are heads, some observed outcomes, and an unobserved target position, compute the conditional probability that the target position is heads.

Assume every sequence containing exactly k heads is equally likely. The observed outcomes are guaranteed to be mutually consistent, and the target position is not observed.

For the original four-coin form, if coin 3 is known to be heads and exactly 2 of the 4 coins are heads, the probability that coin 4 is heads is 1/3. Without the exact-heads condition, fair independent coins would give probability 1/2.

Formal Specification

Implement probability_target_head(n, k, observed, target). n and k are integers. observed is a list of [zero_based_index, is_heads] pairs, where is_heads is a Boolean. target is an unobserved zero-based index. Return a floating-point probability between 0.0 and 1.0.

Constraints

  • 1 <= n <= 500
  • 0 <= k <= n
  • 0 <= observed.length < n
  • Observed indices are distinct and valid
  • The target index is not observed
  • At least one valid assignment exists

Function Signature

def probability_target_head(n, k, observed, target):
Interviewer

Your question is Probability of Coin Heads. 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.