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 result = "[1]";
Attempts:
3 left
💡 Hint
Common Mistakes
Starting with 0 instead of 1
Using multiple digits as the first term
✗ Incorrect
The count-and-say sequence always starts with "1" as the first term.
2fill in blank
mediumComplete the code to iterate through the previous term characters.
DSA C#
for (int i = 0; i < prev.Length; [1]) { }
Attempts:
3 left
💡 Hint
Common Mistakes
Decrementing i causing infinite loop
Skipping characters by incrementing by 2
✗ Incorrect
We increment i by 1 to move through each character in the string.
3fill in blank
hardFix the error in the condition to count consecutive characters.
DSA C#
while (i + 1 < prev.Length && prev[i] [1] prev[i + 1]) { count++; i++; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using '!=' which counts different characters
Using comparison operators like '>' or '<' incorrectly
✗ Incorrect
We count consecutive characters that are equal, so the condition uses '=='.
4fill in blank
hardFill both blanks to append count and character to the result.
DSA C#
result += [1].ToString() + [2];
Attempts:
3 left
💡 Hint
Common Mistakes
Appending the index instead of the character
Appending the result string instead of count
✗ Incorrect
We append the count as a string and then the character from prev at index i.
5fill in blank
hardFill all three blanks to complete the count-and-say function.
DSA C#
public string CountAndSay(int n) {
if (n == 1) return "[1]";
string prev = CountAndSay(n [2] [3]);
string result = "";
int i = 0;
while (i < prev.Length) {
int count = 1;
while (i + 1 < prev.Length && prev[i] == prev[i + 1]) {
count++;
i++;
}
result += count.ToString() + prev[i];
i++;
}
return result;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using '+' instead of '-' in recursion
Returning '0' instead of '1' as base case
✗ Incorrect
The base case returns "1"; the recursive call uses n - 1; the first term is "1".
