Two-Headed Coin After k Heads
Updated
Problem statement
Return the probability that a coin picked uniformly at random from n coins is the two-headed coin, given that it came up heads on all k flips.
Exactly one of the n coins has heads on both sides. The other n - 1 coins are fair.
Note: a fair coin lands heads with probability 1/2 on each flip, independently of other flips.
Function
twoHeadedCoinProbability(n: int, k: int) → doubleExamples
Example 1
n = 1000k = 10return = 0.5061789422The two-headed coin is picked with probability 1/1000 and always shows 10 heads. A fair coin is picked with probability 999/1000 and shows 10 heads with probability 1/1024. So the answer is 1024 / (1024 + 999) = 1024 / 2023, about 0.5061789422.
Example 2
n = 3k = 1return = 0.5000000000The two-headed coin always shows heads, and each of the 2 fair coins shows heads half the time. So the answer is 1 / (1 + 0.5 + 0.5) = 0.5.
Example 3
n = 5k = 0return = 0.2000000000With no flips there is no evidence, so the answer is the chance of picking that coin, 1/5.
Constraints
1 <= n <= 10^90 <= k <= 60- Answers within
10^-9of the correct value are accepted.
More Hudson River Trading Phone Screen questions
- Format a Signed Integer with CommasPHONE SCREEN · Seen Nov 2026
- Order and Batch Open-Close LifecyclePHONE SCREEN · Seen Nov 2026
- Tilting Maze BallPHONE SCREEN · Seen Oct 2026
- Tiered Expiring Item StorePHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
- Mean vs Median: Chance of Missing the CenterPHONE SCREEN · Seen Sep 2026
- Market Data Signal WatcherPHONE SCREEN · Seen Jul 2026
- Integer to String Without Built-insPHONE SCREEN · Seen May 2026
- Contact Tracing Through Timed MeetingsPHONE SCREEN · Seen Nov 2024
- Polydivisible Pandigital NumbersPHONE SCREEN · Seen Feb 2024
- Defragment a Buffer of ObjectsPHONE SCREEN · Seen Nov 2023
Similar questions at other companies
- Parking Fee CalculatorBreeze · PHONE SCREEN
- Merchant Payout Reserve CalculatorStripe · PHONE SCREENStripe interview questions
- Sqrt(x)LinkedIn · PHONE SCREENLinkedIn interview questions
- Automation Pipeline, Part 1: Single WorkerPlaid · PHONE SCREENPlaid interview questions
- Subtract Two Large Number StringsStackAdapt · PHONE SCREENStackAdapt interview questions
- Circular Primes Below a LimitAmazon · PHONE SCREEN
- Palindrome NumberKotak Mahindra Bank · PHONE SCREENKotak Mahindra Bank interview questions
- Streaming AverageXantium · PHONE SCREEN
- Fair Coin from a Biased CoinAnthropic · PHONE SCREENAnthropic interview questions
- Stack Formula InterpreterTypeSafe AI · PHONE SCREEN