Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to push the current element onto the stack.
DSA C#
stack.[1](arr[i]);Attempts:
3 left
💡 Hint
Common Mistakes
Using Pop instead of Push removes elements instead of adding.
Using Peek only views the top element without adding.
✗ Incorrect
We use Push to add the current element to the stack for future comparisons.
2fill in blank
mediumComplete the code to check if the stack is not empty before accessing its top element.
DSA C#
while (stack.Count > 0 && stack.Peek() [1] arr[i])
Attempts:
3 left
💡 Hint
Common Mistakes
Using '>' reverses the logic and causes incorrect popping.
Using '==' or '!=' does not help find greater elements.
✗ Incorrect
We want to pop elements smaller than the current element, so we check if top of stack is less than current.
3fill in blank
hardFix the error in assigning the next greater element when the stack is empty.
DSA C#
result[i] = stack.Count == 0 ? [1] : stack.Peek();
Attempts:
3 left
💡 Hint
Common Mistakes
Using 0 or arr[i] can give wrong results.
Using int.MaxValue is not standard for this problem.
✗ Incorrect
When no greater element exists, we assign -1 as per convention.
4fill in blank
hardFill both blanks to complete the for loop iterating from the end to the start of the array.
DSA C#
for (int i = arr.Length - 1; i [1]= 0; i[2])
Attempts:
3 left
💡 Hint
Common Mistakes
Using '>' excludes index 0.
Using '++' moves forward, not backward.
✗ Incorrect
We start from the last index and move backwards, so i >= 0 and i-- are correct.
5fill in blank
hardFill all three blanks to complete the dictionary comprehension that maps each element to its next greater element.
DSA C#
var nextGreater = new Dictionary<int, int>(); for (int i = 0; i [1] arr.Length; i[2]) { nextGreater[arr[i]] = [3]; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using '--' or '>=' causes wrong loop direction.
Assigning arr[i] instead of result[i] gives wrong mapping.
✗ Incorrect
We loop forward with i < arr.Length and i++ and assign result[i] as the next greater element.
