Grokking the Coding Interview: Patterns for Coding Questions
Ask Author
Back to course home

0% completed

Vote For New Content
Laurensius Hans Santoso
Why do we add last interval to `mergedIntervals` outside the loop?

Laurensius Hans Santoso

May 4, 2023

As described in the title, I don't understand why we add the last interval to mergedIntervals outside the loop? Don't we also evaluate the last interval in intervals inside the for loop? Thank you!

1

0

Comments
Comments
A
Alisha Hweea year ago

It took me awhile to get this too, but it dawned on me when I ran the code without that line of code.

Every time you loop, whether you find a merge or not, you add it to the array. But since you're starting at index 1 (instead of 0), you're actually doing this append ...

On this page