All problems
Geometric Tail Quantile
Problem
For a geometric variable with success probability p, the tail probability is P(X > n) = (1 - p)^n. Return the smallest integer n >= 0 with P(X > n) <= alpha. Return 0 when p = 1 or alpha = 1, and -1 when p is outside (0, 1] or alpha is outside (0, 1].
Hint
Invert the tail with logarithms, then nudge by a step to avoid floating-point edge cases.
Test cases
case 1
in: [0.5,0.5]
expected: 1
case 2
in: [0.5,0.1]
expected: 4
case 3
in: [0.2,0.01]
expected: 21
case 4
in: [1,0.3]
expected: 0
case 5
in: [0.5,0]
expected: -1
Starter codePython
import math
def geometric_tail_quantile(p, alpha):
# Your code here
pass5 test cases · run your code against them in the editor