5 questions to test your understanding
A loop maintains the invariant: 'At the top of each iteration, max_so_far holds the maximum of arr[0..i-1].' The loop runs from i=0 to i=n. What does the invariant tell us after the loop completes?
A developer has a loop that passes most test cases but fails intermittently. What should they do first, according to loop invariant methodology?
A loop invariant must hold true before the very first iteration of the loop executes.
A loop invariant is a condition whose variable values remain constant (unchanged) throughout nearly every iteration.
Explain how thinking about loop invariants can help you design a loop correctly in the first place, rather than just verifying a loop after writing it.