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
Target Heads Probability with Exactly K Heads
00:00
5 left

Target Heads Probability with Exactly K 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

Whenever you're ready, walk me through your approach or go straight to the code. The requirements are in the Question tab.

You need to log in / sign up to run or submit.
Code
Sign up free to run your codeLog inLn 2
Run your code to see test output here.