educative.io

Clarification on inserting the topStart back in the maxStartHeap

If i comment out the line in your solution(line 33):
heappush(maxStartHeap, (topStart, startIndex))
I still get the right output
Can you please give an example where this is absolutely necessary?


Course: Grokking the Coding Interview: Patterns for Coding Questions - Learn Interactively
Lesson: Solution Review: Problem Challenge 1 - Grokking the Coding Interview: Patterns for Coding Questions

1 Like

Hi @asha_alex, Welcome to our Discuss Query Community!
Thank you for contacting us with your query.
As you said that if we comment out line no.33 in our solution we will still get the right output. Yes, we will get the right output unless the boundary conditions are involved. The purpose of the command written heappush(maxStartHeap, (topStart, startIndex)) is to put the interval back as it could be the next interval of other intervals. It is just to cater to the boundary condition to make the solution more generic.

Thank you!

Like I asked,can you please provide an example with intervals? I have not been able to come up with such intervals.

1 Like

Hi @asha_alex,
Sure Assume the following Intervals : (1, 2),(0, 1),(2, 1).

I have attached screenshots for your better understanding. Hope you understand.Thanks!!!


The last interval in your above example (2,1)…The start time is greater than the end time in the same interval. I don’t think this is valid


Course: Grokking the Coding Interview: Patterns for Coding Questions - Learn Interactively
Lesson: Unique Generalized Abbreviations (hard) - Grokking the Coding Interview: Patterns for Coding Questions

1 Like