Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
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
You need to log in / sign up to run or submit.Ln 2
Run your code to see test output here.