GeeksforGeeks » Interview Questions

Google Interview Question for Software Engineer/Developer about Testing

(7 posts)

Tags:

  1. raghu
    guest
    Posted 1 year ago #

    You have a function which returns number of primes b/w 1 to N.

    How will you test it?

  2. Claudio
    guest
    Posted 1 year ago #

    By induction:

     int check(n) {
    
       if (n == 1) return 1;
    
       if (isPrime(n)) return 1 + f(n-1);
    
       return f(n-1);
    }
    
    for (i to N) {
     if (check(n) != f(n)) print("f(n) is bugged");
    }
    
  3. Claudio
    guest
    Posted 1 year ago #

    I've made a typo:

    for (i to N) { .... }

    is

    for(n=1 to N) {....}

  4. elenashutova
    Member
    Posted 1 year ago #

    Modifications to the check() method:
    1. To make it independent from f().
    2. To remove recursion.

      int check(int n)
     {
    	int cnt = 0;
    	while(n > 0)
    	{
    		cnt = (isPrime(n--) ? cnt+1 : cnt);
       }
       return cnt;
    }
    
    int k=N;
    while(k > 0)
    {
        if ( check(k)  !=  f(k))
            print("error in f() for the number %d\n", k);
        k--;
    }
    
  5. bala
    Member
    Posted 1 year ago #

    Both the solution holds good.

    I have a doubt with the solution of Claudio though . If(n==1), shouldn't it return 0 ? Why is it returning 1 ? 1 is not a prime number !

    Correct me if I am wrong

  6. Claudio
    guest
    Posted 1 year ago #

    Bala, you are right ;)

  7. disappearedng
    Member
    Posted 1 year ago #

    I don't know if both of your solution holds..

    isPrime is NOT something that you can just pull out of the air. You probably need O(n) time and O(n) space to do that (cost is for initial processing only).

    Why don't you just call isPrime from 1 to N, then compare with function?


Reply

You must log in to post.

RSS feed for this topic