Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to initialize the stack for indices.
DSA Rust
let mut stack: Vec<usize> = [1];Attempts:
3 left
💡 Hint
Common Mistakes
Using vec![0] initializes stack with one element which is incorrect.
Using Vec::with_capacity(10) reserves space unnecessarily for an empty stack.
✗ Incorrect
We initialize an empty vector to use as a stack for indices.
2fill in blank
mediumComplete the code to push the current index i onto the stack.
DSA Rust
stack.[1](i);Attempts:
3 left
💡 Hint
Common Mistakes
Using insert requires an index and is not typical for stack push.
append is for concatenating vectors, not pushing single elements.
✗ Incorrect
The push method adds an element to the end of the vector, acting like a stack push.
3fill in blank
hardFix the error in the condition to pop from stack when current height is less than height at stack top.
DSA Rust
while !stack.is_empty() && heights[i] [1] heights[*stack.last().unwrap()] {
Attempts:
3 left
💡 Hint
Common Mistakes
Using '>' or '>=' causes incorrect popping behavior.
Using '<=' pops too early or late, breaking logic.
✗ Incorrect
We pop when current height is less than the height at the top of the stack to find smaller boundaries.
4fill in blank
hardFill both blanks to calculate width and update max_area correctly.
DSA Rust
let width = if stack.is_empty() { i } else { i [1] *stack.last().unwrap() [2] 1 }; max_area = max_area.max(height * width);
Attempts:
3 left
💡 Hint
Common Mistakes
Using '+' instead of '-' for width calculation causes wrong width.
Using '*' or '/' are incorrect for index arithmetic.
✗ Incorrect
Width is calculated by subtracting the index at stack top from current index and subtracting 1 to get the correct width.
5fill in blank
hardFill all three blanks to complete the loop that processes remaining stack elements after main iteration.
DSA Rust
while !stack.is_empty() { let height = heights[stack.pop().unwrap()]; let width = if stack.is_empty() { [1] } else { [2] - *stack.last().unwrap() [3] 1 }; max_area = max_area.max(height * width); }
Attempts:
3 left
💡 Hint
Common Mistakes
Using heights.len() for both blanks is correct; mixing with other values causes errors.
Using '+' instead of '-' in last blank causes wrong width.
✗ Incorrect
Width is calculated using the total length minus the last index in stack minus one for correct width after popping.
