Co-Authored-By: Claude Sonnet 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_017ic4b6nxynun5JFivebVb1
34 lines
1.8 KiB
Plaintext
34 lines
1.8 KiB
Plaintext
Eight classic mistakes, each reproduced
|
|
=======================================
|
|
|
|
1. balanced brackets by counting each kind, with no stack
|
|
input "([)]": counts-only = true stack = false
|
|
every count ends at zero, but the ] arrives while the ( is not the innermost open bracket
|
|
|
|
2. min stack that compares boxed Integers with ==
|
|
push [10, 20, 5], pop, getMin(): broken = 10 correct = 10
|
|
push [1000, 2000, 500], pop, getMin(): broken = 500 correct = 1000
|
|
Integer.valueOf caches -128..127, so small values share one object and == happens to work
|
|
|
|
3. min stack that pushes onto the min stack only on a strictly smaller value
|
|
push 3, push 3, pop, getMin(): strict -> NullPointerException: Cannot invoke "java.lang.Integer.intValue()" because the return value of "java.util.Deque.peek()" is null
|
|
correct -> 3 (the stack still holds one 3)
|
|
|
|
4. encoded min stack with int differences instead of long
|
|
push Integer.MIN_VALUE, push Integer.MAX_VALUE, top(): int diffs = -2147483648 long diffs = 2147483647
|
|
MAX_VALUE - MIN_VALUE does not fit in an int, wraps to -1, and -1 looks like "new minimum"
|
|
|
|
5. next greater element popping on <= instead of <
|
|
input [2, 2, 3]: buggy = [2, 3, -1] correct = [3, 3, -1]
|
|
the first 2 is told its next greater element is 2
|
|
|
|
6. largest rectangle without the final height-0 bar
|
|
input [2, 4]: buggy = 0 correct = 4
|
|
nothing ever pops the bars that are still on the stack when the loop ends
|
|
|
|
7. queue from two stacks that pours on every dequeue
|
|
enqueue 1, 2; dequeue; enqueue 3; dequeue: first = 1 (ok), second: broken = 3 correct = 2
|
|
the 3 was poured on top of the 2 that was still waiting in the output stack
|
|
|
|
8. popping an empty structure: both conversions throw NoSuchElementException, as ArrayDeque.pop does
|