GeeksforGeeks » Interview Questions
Google Interview Question for Software Engineer/Developer about Testing
(7 posts)-
You have a function which returns number of primes b/w 1 to N.
How will you test it?
-
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"); } -
I've made a typo:
for (i to N) { .... }
is
for(n=1 to N) {....}
-
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--; } -
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
-
Bala, you are right ;)
-
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.