Ask.com Interview Question
Software Engineer / DevelopersCountry: United States
Interview Type: Written Test
Looks good. I think random number generation more appropriate. Say if rand() returns a floating point numbers between 0.0 to 1.0, if (0.1 <= value < 0.15) we redirect to p3.html and so on. In your case, there will be a situation when user will refresh p1.html 65 times before he skips p5.html.
//Calculate a random number between 0-99;
var random = Math.floor(Math.random()*100);
if(random >= 0 && random < 10) {
//redirect to p2.html
}
else if(random >= 10 && random < 15) {
//redirect to p3.html
}
else if(random >= 15 && random < 35) {
//redirect to p4.html
}
else {
//redirect to p5.html
}
class PageRedirect
- Anonymous January 07, 2013{
private :
const int p2_page_max_limit;
int p2_page_redirected_count;
const int p3_page_max_limit;
int p3_page_redirected_count;
const int p4_page_max_limit;
int p4_page_redirected_count;
const int p5_page_max_limit;
int p5_page_redirected_count;
static int redirectedOrderCount;
public :
PageRedirect() : p2_page_max_limit(10), p3_page_max_limit(5), p4_page_max_limit(20), p5_page_max_limit(65)
{
p2_page_redirected_count = 0;
p3_page_redirected_count = 0;
p4_page_redirected_count = 0;
p5_page_redirected_count = 0;
PageRedirect::redirectedOrderCount = 2; //2 - redirect to p2, 3 - redirect to p3, 4 - redirect to p4, 4 - redirect to p4
}
void RedirectFromP1()
{
if (PageRedirect::redirectedOrderCount == 2)
{
if (p2_page_redirected_count < p2_page_max_limit)
{
p2_page_redirected_count = p2_page_redirected_count + 1;
//Redirect to p2
}
PageRedirect::redirectedOrderCount = PageRedirect::redirectedOrderCount + 1;
}
if (PageRedirect::redirectedOrderCount == 3)
{
if (p3_page_redirected_count < p3_page_max_limit)
{
p3_page_redirected_count = p3_page_redirected_count + 1;
//Redirect to p3
}
PageRedirect::redirectedOrderCount = PageRedirect::redirectedOrderCount + 1;
}
if (PageRedirect::redirectedOrderCount == 4)
{
if (p4_page_redirected_count < p4_page_max_limit)
{
p4_page_redirected_count = p4_page_redirected_count + 1;
//Redirect to p4
}
PageRedirect::redirectedOrderCount = PageRedirect::redirectedOrderCount + 1;
}
if (PageRedirect::redirectedOrderCount == 5)
{
if (p5_page_redirected_count < p5_page_max_limit)
{
p5_page_redirected_count = p5_page_redirected_count + 1;
//Redirect to p5
}
PageRedirect::redirectedOrderCount = PageRedirect::redirectedOrderCount + 1;
}
if (PageRedirect::redirectedOrderCount > 5)
{
PageRedirect::redirectedOrderCount = 2;
}
if ((p2_page_redirected_count + p3_page_redirected_count + p4_page_redirected_count + p5_page_redirected_count) == 100)
{
p2_page_redirected_count = 0;
p3_page_redirected_count = 0;
p4_page_redirected_count = 0;
p5_page_redirected_count = 0;
}
}
};
The above code will redirect to the pages in round robin fashion. p2->p3->p4->p5->p2->.. so on until they reach the limit.
Is this correct implementation? Any other better way to do?