Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8645143
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T12:28:24+00:00 2026-06-12T12:28:24+00:00

This was a question at a programming contest that finished yesterday at interviewstreet: Alice

  • 0

This was a question at a programming contest that finished yesterday at interviewstreet:

Alice and Bob play a game. The operations at round i (i >= 1) is as follows:

  • Alice pays Bob 2 * i – 1 dollars,
  • Alice tosses a biased coin,
  • If the result of the coin was heads for k consecutive rounds, the game stops, otherwise the game continues.

Given k and the probablity that the outcome of a toss is heads (p), your program should find the expected number of dollars Alice pays Bob, and also the expected number of rounds played.

Input

First line of input contains number of test-cases (T <= 50). Each of
the next T lines contain p and k separated by a single space. p is a
decimal number with at most two digits after the decimal point such
that 0.6 <= p <= 1. k is a positive integer such that 0 < k <= 20.

Output

For each test-case, print two integer numbers. First number is the
integer part of the expected number of rounds of game, and the second
number is the integer part of the expected number of dollars Alice
pays Bob.

Sample Input

3

0.6 1

1 20

0.80 8

Sample Output

1 3

20 400

24 976

I had gotten the first part of the problem, i.e the expected number of rounds of the game. I got it with the following code

if __name__ == '__main__':
    t = int(raw_input())   
    while t :
        t -= 1
        temp = str(raw_input())
        p,k = temp.split(' ')
        p = float(p)
        k = int(k)

        #print p,k
        ans  = 0.0
        num = k * (p**k)
        den = 1
        q = 1.0 - p
        for N in range(1,k+1):
            den = den - ((p**(N-1))*q)
            num = num + (N*(p**(N-1))*q)
            #print (N*(q**N))

        print int(num/den)

But the second part of the problem is still puzzling me, i.e the expected number of dollars Alice pays bob. How can expected payoff be calculated?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T12:28:25+00:00Added an answer on June 12, 2026 at 12:28 pm

    You need to average all possible payouts over the probability they occur, even if you know the expected number of rounds. This means it’s more complicated than just computing the payout at the expected stop time. Here are the nitty gritty details:

    Recall that the technical definition of expectation says that if X is a random variable, then the expected value of X is the sum over all possible outcomes w of X(w)*Pr(w). If X takes values in the positive integers, we can rephrase this as the expected value of X is the sum from i=1 to infinity of i*Pr(X=i). In your case, the random variables we’re dealing with are T = time the game stops, and P = payout.

    The expected number rounds is the expectation of T, which is the sum from i=1 to infinity of i*Pr(T=i). Since they only ask for the integer part of the expectation, we can stop summing once i*Pr(T=i) is less than 1/2^i. (The idea on stopping the sum when i*Pr(T=i)<1/2^i is that 1/2^i sums to 1, but you might need to tweak this to avoid underestimating.)

    The expectation of P is slightly more complicated. If the game were to last j rounds, then the payout would be the sum from i=1 to j of 2i-1, which turns out to be j^2. Thus only payouts of the form j^2 can occur, and Pr(P=j^2)=Pr(T=j). So the expected value of P is the sum from i=1 to infinity of i^2 * Pr(P=i^2), which is equal to the sum from i=1 to infinite of i^2*Pr(T=i). Again, we can stop summing once i^2*Pr(T=i) is less than 1/2^i.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This question may not directly relate to programming. I have noticed that the technology
This is a question from some programming contest( i don't know exactly which programming
This question is about programming small microcontrollers without an OS. In particular, I'm interested
I posted this question on Reddit Programming and did not get a single response.
I got this question in a programming test. Do you think this question is
This question is about good programming practices and avoiding potential holes. I read Joshua
This question occured to me while programming a Android application, but it seems to
This question might be naive as I'm new to ColdFusion programming. I have a
This question is more UI/Design-ish than hard-core programming is. Background: I've been coding in
This question may seem daft (I'm a new to 'programming' and should probably stop

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.