Prepin
Log in

Using a biased coin function, create a different function that produces heads or tails with an equal likelihood.

Approach / Explanation

This question presents a function that simulates the toss of a biased coin, indicating that the probabilities of landing on heads and tails are not the same. In contrast, an unbiased coin toss would yield 'Heads' and 'Tails' each with a 50% probability. Your objective is to develop another function that utilizes this biased coin function to ensure an equal likelihood of obtaining both 'Heads' and 'Tails'. To achieve this, you must apply certain programming techniques to balance the biased outcomes. The main points to consider for this question are: Comprehending the concepts of biased and unbiased probabilities. Employing logical computations to derive equal probabilities from the biased coin function provided. Implementing the function with appropriate syntax and algorithm.

Suggested Answer

This can be accomplished by flipping the biased coin two times. The concept is that the probability of obtaining two consecutive heads or two consecutive tails should be the same, whether the coin is manipulated or fair. You only need to ignore the other two outcomes, which are Head, Tail and Tail, Head . In Python: def unbiased_coin ( biased_coin ) : while True : flip1 , flip2 = biased_coin ( ) , biased_coin ( ) if flip1 != flip2 : return flip1 In this Python function, biased_coin is designed to return either Head or Tail , and it is called twice to produce two consecutive results. If the outcomes are not the same, namely 'Head, Tail' or 'Tail, Head', one of them is returned. If they are equal, the process continues until an unequal pair is achieved.

Alternative Answer

Here is a more streamlined version of the solution: def unbiased_coin ( biased_coin ) : while True : flips = ( biased_coin ( ) , biased_coin ( ) ) if flips == ( 'head' , 'tail' ) : return 'head' if flips == ( 'tail' , 'head' ) : return 'tail' This version functions similarly to the original example but offers clearer insight into its operation. If the results are 'Head, Tail', it returns 'Head'; if they are 'Tail, Head', it returns 'Tail'. Hence, the function effectively utilizes the potential outcomes to generate an unbiased result.

Question Details

Difficulty & Category

Medium
technical

Companies

LinkedIn

Product

For CandidatesFor RecruitersCompaniesBrowse JobsInterview QuestionsDeep ProfileSkill AssessmentOpportunity Matching
Prepin.ai

© 2026 Prepin LLC · Philadelphia, PA · All rights reserved.