Bird
Raised Fist0

The following code attempts to add a number to the intervals but contains a subtle bug. Which line causes incorrect behavior when adding a number already covered by an existing interval?

medium🐞 Bug Identification Q14 of Q15
Intervals - Data Stream as Disjoint Intervals
The following code attempts to add a number to the intervals but contains a subtle bug. Which line causes incorrect behavior when adding a number already covered by an existing interval?
AMissing lines checking if val is already covered by existing intervals
BLine calculating i = bisect_left(intervals, [val, val])
CLine checking if intervals is empty and appending [val, val]
DLine popping intervals[i] after merging two intervals
Step-by-Step Solution
Solution:
  1. Step 1: Identify missing coverage check

    The code lacks checks to return early if val is already inside intervals[i-1] or intervals[i].
  2. Step 2: Consequence of missing check

    Without this, duplicates or incorrect merges occur, corrupting intervals.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Missing coverage check causes incorrect behavior [OK]
Quick Trick: Always check if val is already covered before inserting [OK]
Common Mistakes:
MISTAKES
  • Forgetting coverage check
  • Incorrect merge boundaries
  • Popping wrong interval
Trap Explanation:
PITFALL
  • Candidates often overlook coverage checks, assuming bisect_left alone suffices.
Interviewer Note:
CONTEXT
  • Tests ability to spot subtle correctness bugs in interval merging code.
Master "Data Stream as Disjoint Intervals" in Intervals

3 interactive learning modes - each teaches the same concept differently

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Intervals Quizzes