FastPrepTwo-Headed Coin After k Heads
Problems/Two-Headed Coin After k Heads

Two-Headed Coin After k Heads

Hudson River Trading logoHudson River Trading● EasyINTERNPHONE SCREEN

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Reported in a Hudson River Trading algorithm developer intern phone screen on probability. In the interview there were 1000 coins, one of them with two heads, and the picked coin showed heads ten times in a row.

Here the number of coins and the number of flips are inputs, so the interview case is the first example.

Our version is about 90% the same as the reported question.

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) → double

Examples

Example 1

n = 1000k = 10return = 0.5061789422

The 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.5000000000

The 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.2000000000

With no flips there is no evidence, so the answer is the chance of picking that coin, 1/5.

Constraints

  • 1 <= n <= 10^9
  • 0 <= k <= 60
  • Answers within 10^-9 of 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

Interview reports that mention this question

  • Hudson River Trading interviews · 2023

Hudson River Trading interview experiences

  • Hudson River Trading Data Science Interviews: 2 Experiences
  • Hudson River Trading interviews · 2021
  • Hudson River Trading OA · 2021
  • Hudson River Trading interviews · 2022
  • Hudson River Trading OA · 2022
All Hudson River Trading interview questionsSee Hudson River Trading hiring insights
Saved
class Solution {
    public double twoHeadedCoinProbability(int n, int k) {
        // Write your code here.
    }
}
Checking account…