Code Room
CodingHardcod-g713
Subject Probability dpLevel Senior–Staff~30 minCommon in Algorithms & data structures interviewsIndustries Software development

Question

A token starts at position 0 on the number line. Each step it moves +1 with probability p and -1 with probability (1-p), where p is given as a float. Return the probability, rounded to 6 decimal places, that the token reaches position +a before it reaches position -b (a and b are positive integers; the walk is absorbed at +a or -b). Constraints: 1 <= a, b <= 1000, 0.0 < p < 1.0.

Implement
reach_a_before_neg_b(a: int, b: int, p: float) → float
Examples
in[1,1,0.5]out0.5
What a strong answer looks like

State your approach and its time/space complexity out loud before you optimize. Handle the edge cases (empty input, duplicates, overflow), and say why you chose this over the brute force. Green tests are the floor, not the grade.

Vibe coding: describe the solution in plain language (or narrate it) and the coach grades your approach. Generating runnable code from your description is coming next.

Run or narrate your approach, then ask the coach.