CtCI-6th-Edition icon indicating copy to clipboard operation
CtCI-6th-Edition copied to clipboard

Q3_01_Three_in_One - MultiStack.java

Open aflar opened this issue 9 years ago • 1 comments

Hello,

I'm trying to understand code from MultiStack.java file. Everything seems to be good, except one thing. Please, take a look at line 24. In what cases if clause could be true?

According to your code, method isWithinStackCapacity called only at line 116 with param index, which came from lastCapacityIndex function call. But lastCapacityIndex function did not return negatives values or values that more or equal than values.length. So maybe if clause at line 24 is redundant?

Thank you!

aflar avatar Sep 20 '16 11:09 aflar

Yeah, I think you're right; the author does a lot of "defensive" checks in these solutions. In my opinion these should have been assertions instead. That is, rather than if(bad) return;, simply do assert !bad;. Oh well, it's not my book; I don't get a vote.

Markvy avatar May 25 '20 17:05 Markvy