Tuesday, May 6, 2014

How many time function gets called?

Problem

Consider the function below:
void foo1()
{
  if(A < B)
    Then { }
  else
   if(C < D)
     then foo2()

}

How many time foo2 () would get called given A < B 25% of the times and C < D 75% of the times and foo1 () is called 5000 times 

Solution
This is the question of basic probability. As foo2() occurs in 2nd branch of if else, the probability of it being called is .75 or 75%. But C < D only 75% chance of this 75% chance.

So total probability = .75 * .75 =  .5625
So number of times = 5000 * 5625 = 2812.5

Reactions:

0 comments:

Post a Comment