Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to initialize the stack for indices.
DSA Go
stack := make([]int, 0) for i := 0; i < len(heights); i++ { for len(stack) > 0 && heights[stack[len(stack)-1]] > heights[i] { top := stack[len(stack)-1] stack = stack[:len(stack)-1] width := i if len(stack) > 0 { width = i - stack[len(stack)-1] - 1 } area := heights[top] * width if area > maxArea { maxArea = area } } stack = append(stack, [1]) }
Attempts:
3 left
💡 Hint
Common Mistakes
Appending the height value instead of the index.
Appending the maxArea variable instead of index.
✗ Incorrect
We append the current index 'i' to the stack to keep track of histogram bars.
2fill in blank
mediumComplete the code to calculate the width when the stack is not empty.
DSA Go
width := i if len(stack) > 0 { width = i - [1] - 1 }
Attempts:
3 left
💡 Hint
Common Mistakes
Using the popped top index instead of the new top of the stack.
Using height values instead of indices.
✗ Incorrect
Width is calculated using the index at the top of the stack after popping.
3fill in blank
hardFix the error in the final loop to process remaining bars in the stack.
DSA Go
n := len(heights) for len(stack) > 0 { top := stack[len(stack)-1] stack = stack[:len(stack)-1] width := n if len(stack) > 0 { width = n - [1] - 1 } area := heights[top] * width if area > maxArea { maxArea = area } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using the popped index 'top' instead of the new stack top.
Using the total length 'n' directly without adjustment.
✗ Incorrect
Width is calculated using the new top of the stack after popping, not the popped top.
4fill in blank
hardFill both blanks to complete the function signature and return statement.
DSA Go
func [1](heights []int) int { maxArea := 0 stack := make([]int, 0) for i := 0; i < len(heights); i++ { for len(stack) > 0 && heights[stack[len(stack)-1]] > heights[i] { top := stack[len(stack)-1] stack = stack[:len(stack)-1] width := i if len(stack) > 0 { width = i - stack[len(stack)-1] - 1 } area := heights[top] * width if area > maxArea { maxArea = area } } stack = append(stack, i) } n := len(heights) for len(stack) > 0 { top := stack[len(stack)-1] stack = stack[:len(stack)-1] width := n if len(stack) > 0 { width = n - stack[len(stack)-1] - 1 } area := heights[top] * width if area > maxArea { maxArea = area } } return [2] }
Attempts:
3 left
💡 Hint
Common Mistakes
Returning the wrong variable like 'area' or 'heights'.
Using a generic function name that doesn't match the task.
✗ Incorrect
The function is named 'largestRectangleArea' and returns the maximum area found.
5fill in blank
hardFill all three blanks to complete the code that calculates area inside the loop.
DSA Go
for len(stack) > 0 && heights[stack[len(stack)-1]] > heights[i] { top := stack[len(stack)-1] stack = stack[:len(stack)-1] width := [1] if len(stack) > 0 { width = i - [2] - 1 } area := heights[[3]] * width if area > maxArea { maxArea = area } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'len(stack)' instead of the top index for width calculation.
Using wrong index for height in area calculation.
✗ Incorrect
Width starts as current index 'i', adjusted by the new top of stack; area uses height at 'top' index.
