Given a function “f” in which 0 occurs with probability 0.4 and 1 occurs with probability 0.6. Using function “f” deduce a new function “f1” such that both 0 and 1 occurs with probability 0.5.
Solution:
Run the function f() two times. The possible outcomes are
00 with probability 0.4*0.4 11 with probability 0.6*0.6 01 with probability 0.4*0.6 10 with probability 0.6*0.4
Notice that the probabilities for 01 and 10 are the same. So create a new function f1() such that
f1(): first = f() second = f() if (first == 0 and second == 0) or (first == 1 or second == 1):
discard and run again else: if first == 0 and second == 1: return 0 else: return 1
Complexity:
time - O(1)Links and credits:
space - O(1)
http://www.geeksforgeeks.org/amazon-interview-set-64-campus-sde/
No comments:
Post a Comment