Hello Everyone, I am 1 year of experience in C++ and I have faced an interview on Facebook. The interviewer asked me, Can you explain how to find the nth term in Count and say sequence. Can anyone suggest me the syntax of this pattern or suggest me some tips for my upcoming second round of interview in Facebook.
// Find n'th term by generating all terms from 3 to
// n-1. Every term is generated using previous term
string str = "11"; // Initialize previous term
for(inti = 3; i<=n; i++)
{
// In below for loop, previous character
// is processed in current iteration. That
// is why a dummy character is added to make
// sure that loop runs one extra iteration.
str += '$';
intlen = str.length();
intcnt = 1; // Initialize count of matching chars
string tmp = ""; // Initialize i'th term in series
// Process previous term to find the next term
for(intj = 1; j < len; j++)
{
// If current character does't match
if(str[j] != str[j-1])
{
// Append count of str[j-1] to temp
tmp += cnt + '0';
// Append str[j-1]
tmp += str[j-1];
// Reset count
cnt = 1;
}
// If matches, then increment count of matching
// characters
elsecnt++;
}
// Update str
str = tmp;
}
returnstr;
}
// Driver program
intmain()
{
intN = 3;
cout << countnndSay(N) << endl;
return0;
}
To know more interview question like this visit here.
Regarding for nth term in Count and Say sequence.
× Après avoir cliqué sur "Répondre" vous serez invité à vous connecter pour que votre message soit publié.
× Attention, ce sujet est très ancien. Le déterrer n'est pas forcément approprié. Nous te conseillons de créer un nouveau sujet pour poser ta question.