employer cover photo
employer logo
employer logo

Clearwater Analytics (CWAN)

Engaged Employer

Clearwater Analytics (CWAN) Interview Question

Write a random method that gives numbers between 1-7 using a random5 method (which gives numbers between 1-5). The results should be randomly distributed. How do you test it?

Interview Answer

Anonymous

Feb 21, 2013

int Random7() { do { a = Random5(); b = Random5(); escape = false; if ( a== 1 ) escape = true; if ( a==2 && b<=2 ) escape; }while( !escape ); return (a-1)*5+b; '} To test, run a large set of test, say 1 Billion and store the frequency in an array. The frequencies should be close to each other, say within a factor of 1.05 of each other.