Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to initialize the first term of the count-and-say sequence.
DSA C++
string countAndSay(int n) {
string result = [1];
// rest of the code
return result;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using an empty string instead of "1".
Starting with "0" or "2" which are incorrect.
✗ Incorrect
The count-and-say sequence starts with "1" as the first term.
2fill in blank
mediumComplete the code to iterate from the second term up to the nth term.
DSA C++
for (int i = 2; i <= [1]; i++) { // generate next term }
Attempts:
3 left
💡 Hint
Common Mistakes
Using the loop variable i as the limit.
Starting from 1 instead of 2.
✗ Incorrect
We iterate from 2 to n to build each term of the sequence.
3fill in blank
hardFix the error in the code that counts consecutive characters in the current term.
DSA C++
for (int j = 1; j < current.size(); j++) { if (current[j] == current[j - 1]) { count[1]; } else { // append count and character count = 1; } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using decrement operator -- instead of increment.
Assigning count instead of incrementing.
✗ Incorrect
We increment count by one when consecutive characters match.
4fill in blank
hardFill both blanks to append the count and character to the next term string.
DSA C++
next += to_string([1]); next += [2];
Attempts:
3 left
💡 Hint
Common Mistakes
Appending the wrong character index.
Appending the loop variable instead of count.
✗ Incorrect
We append the count and the character before the current one to build the next term.
5fill in blank
hardFill in the blanks to update the current term and reset count for the next iteration.
DSA C++
current = [1]; count = [2]; // continue loop
Attempts:
3 left
💡 Hint
Common Mistakes
Not updating current to next.
Resetting count incorrectly.
✗ Incorrect
We set current to next and reset count to 1 for the next iteration.
