Skip to content

Conversation

@BrianLusina
Copy link
Owner

@BrianLusina BrianLusina commented Jan 5, 2026

Describe your change:

Contains algorithm to count minimum number of intervals to remove from a given list of intervals that will make the intervals non-overlapping

Also:

  • insert interval solution write up
  • Add an algorithm?
  • Fix a bug or typo in an existing algorithm?
  • Documentation change?

Checklist:

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized.
  • I know that pull requests will not be merged if they fail the automated tests.
  • This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
  • All new Python files are placed inside an existing directory.
  • All filenames are in all lowercase characters with no spaces or dashes.
  • All functions and variable names follow Python naming conventions.
  • All function parameters and return values are annotated with Python type hints.
  • All functions have doctests that pass the automated testing.
  • All new algorithms have a URL in its comments that points to Wikipedia or other similar explanation.
  • If this pull request resolves one or more open issues then the commit message contains Fixes: #{$ISSUE_NO}.

Summary by CodeRabbit

  • New Features

    • Added Non-Overlapping Intervals algorithm with two implementation variants (in-place and non-mutating approaches)
  • Documentation

    • Added comprehensive documentation for Non-Overlapping Intervals problem including complexity analysis (O(n log n) time, O(1) space)
    • Enhanced Insert Interval documentation with detailed three-phase solution explanation and diagrams
  • Tests

    • Added unit tests for Non-Overlapping Intervals covering multiple scenarios and edge cases

✏️ Tip: You can customize this high-level summary in your review settings.

@BrianLusina BrianLusina self-assigned this Jan 5, 2026
@BrianLusina BrianLusina added enhancement Algorithm Algorithm Problem Datastructures Datastructures Documentation Documentation Updates Sorting Contains sorting in the algorithm Array Array data structure Intervals labels Jan 5, 2026
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Jan 5, 2026

📝 Walkthrough

Walkthrough

This PR introduces a complete solution for the "Count Non-Overlapping Intervals" problem, including algorithm implementation with two variants, comprehensive documentation, and parameterized tests. It also updates the insert_interval documentation with detailed phase-based solution explanation.

Changes

Cohort / File(s) Summary
Directory & Documentation
DIRECTORY.md, algorithms/intervals/count_non_overlapping_intervals/README.md, algorithms/intervals/insert_interval/README.md
Added new subsection entry in directory index; created new README documenting the non-overlapping intervals problem with greedy approach explanation (sorting by end time, O(n log n) time, O(1) space); updated insert_interval README with three-phase solution explanation and diagrams
Algorithm Implementation
algorithms/intervals/count_non_overlapping_intervals/__init__.py
New module with two public functions: count_min_non_overlapping_intervals() (in-place sorting) and count_min_non_overlapping_intervals_2() (non-mutating, uses sorted copy); both implement greedy algorithm counting maximum non-overlapping intervals, returning total intervals minus count
Test Suite
algorithms/intervals/count_non_overlapping_intervals/test_count_min_non_overlapping_intervals.py
New test module with parameterized test cases covering both implementations; defines CountMinNonOverlappingIntervalsTestCase with parameterized methods testing multiple scenarios including edge cases, overlapping intervals, and sequential intervals

Poem

🐰 Intervals aligned, no overlaps found,
A greedy hop sorts end-times 'round,
Two functions dance, one mutates with care,
Tests parameterized everywhere!
The rabbit hops through sorted array—
Non-overlapping intervals here to stay! 🎉

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

Pre-merge checks and finishing touches

❌ Failed checks (2 warnings)
Check name Status Explanation Resolution
Description check ⚠️ Warning Description covers the main change but marks 'Fix a bug' and multiple other checkboxes as checked when the PR only adds features and documentation, creating inconsistencies with the actual changeset. Uncheck unrelated checkboxes (only 'Add an algorithm' and 'Documentation change' should be checked); clarify that the 'insert interval' write-up is a separate concern and consider raising a separate PR for it per guidelines.
Docstring Coverage ⚠️ Warning Docstring coverage is 50.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (1 passed)
Check name Status Explanation
Title check ✅ Passed Title clearly describes the main addition: a feature for counting minimum non-overlapping intervals, which matches the core algorithmic change in the changeset.
✨ Finishing touches
  • 📝 Generate docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (5)
DIRECTORY.md (1)

140-141: Indentation style matches codebase convention but diverges from strict Markdown spec.

Lines 140–141 use 4 and 6 spaces respectively, consistent with all other entries in the Intervals section. However, markdownlint expects 2 and 4 spaces per MD007. Since the entire file follows the 4/6 convention, this is not a new inconsistency, but consider aligning with standard Markdown guidelines in a future refactor.

algorithms/intervals/count_non_overlapping_intervals/README.md (1)

56-60: Minor formatting and clarity improvements needed in complexity analysis.

Line 56: O(n * logn) should use consistent spacing: O(n * log n).

Line 60 states "We only initialize two extra variables" but doesn't explicitly complete the notation. Suggest: "O(1) where we only initialize two extra variables..." for clarity and consistency with the Time Complexity section format.

algorithms/intervals/count_non_overlapping_intervals/__init__.py (2)

4-23: Consider adding input validation for interval structure.

The function assumes each interval contains exactly two elements [start, end] and that start <= end. Invalid inputs like [[1]], [[1, 2, 3]], or [[5, 1]] could cause IndexError or produce incorrect results.

Consider adding validation at the start of the function:

🔎 Suggested input validation
 def count_min_non_overlapping_intervals(intervals: List[List[int]]) -> int:
     """
     Counts the minimum number of intervals that must be removed to make the intervals non-overlapping. Modifies the
     input list in place
     Args:
         intervals (List[List[int]]): The intervals to check
     Returns:
         int: number of intervals to remove
     """
     if not intervals:
         return 0
+    # Validate interval structure
+    for interval in intervals:
+        if len(interval) != 2:
+            raise ValueError(f"Each interval must have exactly 2 elements, got {len(interval)}")
+        if interval[0] > interval[1]:
+            raise ValueError(f"Interval start must be <= end, got {interval}")
     intervals.sort(key=lambda x: x[1])
     end = intervals[0][1]
     count = 1
     for i in range(1, len(intervals)):
         # Non-overlapping interval found
         if intervals[i][0] >= end:
             end = intervals[i][1]
             count += 1
     return len(intervals) - count

26-50: Consider adding input validation for interval structure.

Similar to the first implementation, this function assumes well-formed intervals. Apply the same validation logic to ensure robustness.

🔎 Suggested input validation
 def count_min_non_overlapping_intervals_2(intervals: List[List[int]]) -> int:
     """
     Counts the minimum number of intervals that must be removed to make the intervals non-overlapping. Does not modify
      the input list in place, this instead uses a sorted copy of the input intervals
     Args:
         intervals (List[List[int]]): The intervals to check
     Returns:
         int: number of intervals to remove
     """
     if not intervals:
         return 0
+    # Validate interval structure
+    for interval in intervals:
+        if len(interval) != 2:
+            raise ValueError(f"Each interval must have exactly 2 elements, got {len(interval)}")
+        if interval[0] > interval[1]:
+            raise ValueError(f"Interval start must be <= end, got {interval}")

     sorted_intervals = sorted(intervals, key=lambda x: x[1])

     end = sorted_intervals[0][1]
     count = 1

     for current_interval in sorted_intervals[1:]:
         current_interval_start, current_interval_end = current_interval
         # Non-overlapping interval found
         if current_interval_start >= end:
             end = current_interval_end
             count += 1

     return len(sorted_intervals) - count
algorithms/intervals/count_non_overlapping_intervals/test_count_min_non_overlapping_intervals.py (1)

10-19: Consider adding edge case test coverage.

The test cases cover various overlapping scenarios well, but could benefit from additional edge cases:

  • Single interval: [[1, 5]] → expected 0
  • Empty list: [] → expected 0 (implementation handles this)
  • Negative intervals: [[-5, -1], [-3, 0], [1, 3]]
  • Large gaps: [[1, 2], [100, 200]] → expected 0
🔎 Suggested additional test cases
 COUNT_NON_OVERLAPPING_INTERVALS_TEST_CASES = [
     ([[1, 3], [5, 8], [4, 10], [11, 13]], 1),
     ([[1, 2], [2, 3], [3, 4], [1, 3]], 1),
     ([[1, 2], [1, 2], [1, 2]], 2),
     ([[1, 2], [2, 3]], 0),
     ([[1, 5], [2, 3], [3, 4], [4, 6]], 1),
     ([[1, 3], [3, 5], [4, 6], [5, 7]], 1),
     ([[1, 3], [2, 4], [3, 5]], 1),
     ([[0, 2], [1, 3], [2, 4], [3, 5], [4, 6]], 2),
+    # Edge cases
+    ([[1, 5]], 0),  # Single interval
+    ([], 0),  # Empty list
+    ([[-5, -1], [-3, 0], [1, 3]], 1),  # Negative intervals
+    ([[1, 2], [100, 200]], 0),  # Large gaps
 ]
📜 Review details

Configuration used: defaults

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between be0e4fd and 10340c2.

⛔ Files ignored due to path filters (20)
  • algorithms/intervals/count_non_overlapping_intervals/images/examples/count_non_overlapping_intervals_example_1.png is excluded by !**/*.png
  • algorithms/intervals/count_non_overlapping_intervals/images/solutions/count_non_overlapping_intervals_solution_1.png is excluded by !**/*.png
  • algorithms/intervals/count_non_overlapping_intervals/images/solutions/count_non_overlapping_intervals_solution_2.png is excluded by !**/*.png
  • algorithms/intervals/count_non_overlapping_intervals/images/solutions/count_non_overlapping_intervals_solution_3.png is excluded by !**/*.png
  • algorithms/intervals/count_non_overlapping_intervals/images/solutions/count_non_overlapping_intervals_solution_4.png is excluded by !**/*.png
  • algorithms/intervals/count_non_overlapping_intervals/images/solutions/count_non_overlapping_intervals_solution_5.png is excluded by !**/*.png
  • algorithms/intervals/count_non_overlapping_intervals/images/solutions/count_non_overlapping_intervals_solution_6.png is excluded by !**/*.png
  • algorithms/intervals/count_non_overlapping_intervals/images/solutions/count_non_overlapping_intervals_solution_7.png is excluded by !**/*.png
  • algorithms/intervals/count_non_overlapping_intervals/images/solutions/count_non_overlapping_intervals_solution_8.png is excluded by !**/*.png
  • algorithms/intervals/count_non_overlapping_intervals/images/solutions/count_non_overlapping_intervals_solution_9.png is excluded by !**/*.png
  • algorithms/intervals/insert_interval/images/solutions/insert_interval_solution_1.png is excluded by !**/*.png
  • algorithms/intervals/insert_interval/images/solutions/insert_interval_solution_10.png is excluded by !**/*.png
  • algorithms/intervals/insert_interval/images/solutions/insert_interval_solution_2.png is excluded by !**/*.png
  • algorithms/intervals/insert_interval/images/solutions/insert_interval_solution_3.png is excluded by !**/*.png
  • algorithms/intervals/insert_interval/images/solutions/insert_interval_solution_4.png is excluded by !**/*.png
  • algorithms/intervals/insert_interval/images/solutions/insert_interval_solution_5.png is excluded by !**/*.png
  • algorithms/intervals/insert_interval/images/solutions/insert_interval_solution_6.png is excluded by !**/*.png
  • algorithms/intervals/insert_interval/images/solutions/insert_interval_solution_7.png is excluded by !**/*.png
  • algorithms/intervals/insert_interval/images/solutions/insert_interval_solution_8.png is excluded by !**/*.png
  • algorithms/intervals/insert_interval/images/solutions/insert_interval_solution_9.png is excluded by !**/*.png
📒 Files selected for processing (5)
  • DIRECTORY.md
  • algorithms/intervals/count_non_overlapping_intervals/README.md
  • algorithms/intervals/count_non_overlapping_intervals/__init__.py
  • algorithms/intervals/count_non_overlapping_intervals/test_count_min_non_overlapping_intervals.py
  • algorithms/intervals/insert_interval/README.md
🧰 Additional context used
🧬 Code graph analysis (1)
algorithms/intervals/count_non_overlapping_intervals/test_count_min_non_overlapping_intervals.py (1)
algorithms/intervals/count_non_overlapping_intervals/__init__.py (2)
  • count_min_non_overlapping_intervals (4-23)
  • count_min_non_overlapping_intervals_2 (26-50)
🪛 LanguageTool
algorithms/intervals/count_non_overlapping_intervals/README.md

[grammar] ~56-~56: Ensure spelling is correct
Context: ...y Analysis #### Time Complexity O(n * logn) where n is the number of intervals. The...

(QB_NEW_EN_ORTHOGRAPHY_ERROR_IDS_1)

algorithms/intervals/insert_interval/README.md

[style] ~63-~63: Using many exclamation marks might seem excessive (in this case: 4 exclamation marks for a text that’s 2680 characters long)
Context: ...lutions/insert_interval_solution_8.png) ![Solution 9](./images/solutions/insert_i...

(EN_EXCESSIVE_EXCLAMATION)

🪛 markdownlint-cli2 (0.18.1)
DIRECTORY.md

140-140: Unordered list indentation
Expected: 2; Actual: 4

(MD007, ul-indent)


141-141: Unordered list indentation
Expected: 4; Actual: 6

(MD007, ul-indent)

algorithms/intervals/insert_interval/README.md

38-38: Reference links and images should use a label that is defined
Missing link or image reference definition: "1"

(MD052, reference-links-images)


47-47: Reference links and images should use a label that is defined
Missing link or image reference definition: "0"

(MD052, reference-links-images)

🔇 Additional comments (3)
algorithms/intervals/insert_interval/README.md (1)

26-75: Comprehensive solution writeup enhances interval insertion documentation.

The three-phase explanation (lines 26–62) with visual aids clearly describes the algorithm. Complexity analysis (lines 67–75) correctly identifies O(n) time and O(n) space.

Note: Markdownlint warnings for reference links (MD052) are false positives—they flag array indexing syntax [i][0] and [i][1] as markdown references. Similarly, the exclamation marks flagged by LanguageTool are part of image alt text syntax, not excessive punctuation.

algorithms/intervals/count_non_overlapping_intervals/test_count_min_non_overlapping_intervals.py (2)

27-28: Excellent use of deepcopy for the mutating function test.

The deepcopy ensures that each parameterized test case starts with fresh data, preventing the in-place sort from affecting subsequent test iterations.


4-4: No action needed. Version 0.9.0 of parameterized is the current latest release and has no known security vulnerabilities.

@BrianLusina BrianLusina merged commit 0353c09 into main Jan 5, 2026
5 of 6 checks passed
@coderabbitai coderabbitai bot mentioned this pull request Jan 8, 2026
14 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Algorithm Algorithm Problem Array Array data structure Datastructures Datastructures Documentation Documentation Updates enhancement Intervals Sorting Contains sorting in the algorithm

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants