Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to slide the window by moving the start index forward.
DSA C++
start = 0; for (int end = 0; end < n; ++end) { if (end - start + 1 > k) { start [1]; } }
Attempts:
3 left
💡 Hint
Common Mistakes
Decreasing start index instead of increasing it.
Using post-increment which works but is less clear here.
✗ Incorrect
To slide the window forward, we increase the start index by one using ++start.
2fill in blank
mediumComplete the code to add the current element to the window sum.
DSA C++
int window_sum = 0; for (int end = 0; end < n; ++end) { window_sum [1] arr[end]; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using subtraction instead of addition.
Using multiplication or division which changes the sum incorrectly.
✗ Incorrect
We add the current element to the window sum using the += operator.
3fill in blank
hardFix the error in the condition to check if the window size equals k.
DSA C++
if (end - start [1] k - 1) { // process window }
Attempts:
3 left
💡 Hint
Common Mistakes
Using > or < instead of == causes wrong window size checks.
Forgetting the -1 adjustment in the condition.
✗ Incorrect
The window size is (end - start + 1). To check if it equals k, use end - start == k - 1.
4fill in blank
hardFill both blanks to correctly update the window sum when sliding the window.
DSA C++
window_sum [1] arr[end]; window_sum [2] arr[start];
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong operators causes incorrect sums.
Forgetting to subtract the old element.
✗ Incorrect
Add the new element with += and subtract the old element with -= to update the sum.
5fill in blank
hardFill all three blanks to create a map of word lengths for words longer than 3 characters.
DSA C++
std::map<std::string, int> lengths; for (const auto& word : words) { if (word.size() [1] 3) { lengths[[2]] = [3]; } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using <= instead of > in condition.
Swapping key and value in the map.
✗ Incorrect
Check if word size is greater than 3, then map word to its length.
