PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep
|Home/Coding & Algorithms/Qube

Compute expected days until no whole pills

Last updated: Mar 29, 2026

Quick Overview

This question evaluates understanding of probability and expected value, modeling of state-based stochastic processes (Markov chains), and the ability to represent state transitions for computing hitting times in an algorithmic context.

  • hard
  • Qube
  • Coding & Algorithms
  • Software Engineer

Compute expected days until no whole pills

Company: Qube

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Technical Screen

A bottle initially contains **H half-pills** and **W whole pills**. Each day: 1. You randomly select **one pill uniformly** from the bottle. 2. If it is a **half-pill**, you consume it entirely (it is removed). 3. If it is a **whole pill**, you consume **half** and put the **remaining half** back into the bottle. (So selecting a whole pill decreases the number of whole pills by 1 and increases the number of half-pills by 1.) You always consume exactly **half a pill per day**. Task: **Compute the expected number of days until the bottle contains no whole pills** (i.e., only half-pills remain). ### Input - Two integers `H` and `W`. ### Output - A floating-point number: the expected number of days until `W = 0`. ### Example instance - `H = 25`, `W = 25`. ### Constraints (for your implementation) - You may assume `0 ≤ H, W ≤ 200` (or similar), and you should output the expectation with reasonable precision (e.g., `1e-6`).

Quick Answer: This question evaluates understanding of probability and expected value, modeling of state-based stochastic processes (Markov chains), and the ability to represent state transitions for computing hitting times in an algorithmic context.

Qube logo
Qube
Jan 15, 2026, 12:00 AM
Software Engineer
Technical Screen
Coding & Algorithms
3
0
Loading...

A bottle initially contains H half-pills and W whole pills.

Each day:

  1. You randomly select one pill uniformly from the bottle.
  2. If it is a half-pill , you consume it entirely (it is removed).
  3. If it is a whole pill , you consume half and put the remaining half back into the bottle. (So selecting a whole pill decreases the number of whole pills by 1 and increases the number of half-pills by 1.)

You always consume exactly half a pill per day.

Task: Compute the expected number of days until the bottle contains no whole pills (i.e., only half-pills remain).

Input

  • Two integers H and W .

Output

  • A floating-point number: the expected number of days until W = 0 .

Example instance

  • H = 25 , W = 25 .

Constraints (for your implementation)

  • You may assume 0 ≤ H, W ≤ 200 (or similar), and you should output the expectation with reasonable precision (e.g., 1e-6 ).

Submit Your Answer to Earn 20XP

Sign in to leave a comment

Loading comments...

Browse More Questions

More Coding & Algorithms•More Qube•More Software Engineer•Qube Software Engineer•Qube Coding & Algorithms•Software Engineer Coding & Algorithms
PracHub

Master your tech interviews with 8,000+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.