Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to declare a dictionary to count character frequencies.
DSA C#
var frequency = new Dictionary<char, int>[1];Attempts:
3 left
💡 Hint
Common Mistakes
Using curly braces '{}' instead of parentheses '()' to instantiate the dictionary.
Using square brackets '[]' which are for arrays or indexers.
✗ Incorrect
In C#, to create a new Dictionary, you use parentheses '()' after the type.
2fill in blank
mediumComplete the code to check if the dictionary contains a character key.
DSA C#
if (frequency.[1](c)) { frequency[c]++; } else { frequency[c] = 1; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using ContainsValue which checks values, not keys.
Using non-existent methods like HasKey or Exists.
✗ Incorrect
The Dictionary method to check if a key exists is ContainsKey.
3fill in blank
hardFix the error in the code to correctly update the frequency count.
DSA C#
frequency[c] [1] frequency[c] + 1;
Attempts:
3 left
💡 Hint
Common Mistakes
Using '==' which is a comparison operator.
Using '++' incorrectly on the dictionary value.
✗ Incorrect
To update the value, use '=' to assign the new value. '==' is a comparison, '+=' adds and assigns, '++' increments but cannot be used like this.
4fill in blank
hardFill both blanks to create a frequency counter for characters in a string.
DSA C#
foreach (char [1] in input) { if (frequency.[2](c)) { frequency[c]++; } else { frequency[c] = 1; } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'ch' as loop variable but 'c' inside the loop causing errors.
Using ContainsValue instead of ContainsKey.
✗ Incorrect
The loop variable is 'c' and the method to check keys is ContainsKey.
5fill in blank
hardFill all three blanks to create a frequency counter dictionary comprehension equivalent.
DSA C#
var frequency = input.GroupBy([1] => [2]).ToDictionary(g => g.Key, g => g.[3]());
Attempts:
3 left
💡 Hint
Common Mistakes
Using Length which is not a method on groups.
Using inconsistent lambda variable names.
✗ Incorrect
GroupBy uses 'c' as the variable, ToDictionary keys by g.Key, and counts elements with Count().
