Skip to content

Conversation

@BrianLusina
Copy link
Owner

@BrianLusina BrianLusina commented Nov 12, 2025

Describe your change:

Adds an algorithm to count how many subarrays are within the given array that satisfy the fixed bound problem

  • 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 "Count Subarrays with Fixed Bounds" algorithm to the Arrays collection to count contiguous subarrays whose minimum equals a given min and maximum equals a given max.
  • Documentation
    • Added a README describing the problem, constraints, examples, and usage notes.
  • Tests
    • Added unit tests covering mixed, edge, and equal-bound scenarios.
@BrianLusina BrianLusina self-assigned this Nov 12, 2025
@BrianLusina BrianLusina added Algorithm Algorithm Problem Array Array data structure labels Nov 12, 2025
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Nov 12, 2025

Walkthrough

Adds a new datastructures module implementing an algorithm to count subarrays whose minimum equals min_k and maximum equals max_k, with README, tests, and a DIRECTORY entry linking the new problem.

Changes

Cohort / File(s) Summary
Directory index & docs
DIRECTORY.md, datastructures/arrays/subarrays_with_fixed_bounds/README.md
Added directory entry and README describing the "Count Subarrays with Fixed Bounds" problem, constraints, and examples.
Implementation
datastructures/arrays/subarrays_with_fixed_bounds/__init__.py
New function count_subarrays(nums: List[int], min_k: int, max_k: int) -> int that iterates input, tracks last indices of min_k, max_k, and invalid values, and accumulates the number of valid subarrays ending at each index. Includes input validation and docstring examples.
Tests
datastructures/arrays/subarrays_with_fixed_bounds/test_subarrays_with_fixed_bounds.py
New unittest module with six test cases exercising mixed inputs, edge conditions, and equal-bound scenarios; includes unittest.main() entry point.

Sequence Diagram(s)

sequenceDiagram participant T as Test / Caller participant F as count_subarrays(nums, min_k, max_k) rect rgba(200,240,200,0.4) T->>F: call with nums, min_k, max_k end Note right of F: initialize counters\nlast_min = last_max = last_invalid = -1\nresult = 0 loop iterate nums[i] F->>F: check nums[i] against min_k/max_k alt nums[i] < min_k or nums[i] > max_k F->>F: last_invalid = i else if nums[i] == min_k F->>F: last_min = i else if nums[i] == max_k F->>F: last_max = i end F->>F: add max(0, min(last_min, last_max) - last_invalid) to result end F-->>T: return result 
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

  • Pay attention to: correctness of index-tracking logic in __init__.py (edge cases when min_k==max_k), handling of empty inputs, and boundary conditions in tests.
  • Verify README examples align with implemented behavior and tests.

Poem

🐰 In arrays I hop and peek,
I track min and max each week,
With indices neat and fast,
I count the subarrays that last,
Hooray — bounds found, problem complete! 🥕

Pre-merge checks and finishing touches

✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title 'feat(arrays) subarrays with fixed bounds' clearly and specifically describes the main change: adding an algorithm for the subarrays with fixed bounds problem.
Description check ✅ Passed The pull request description covers the main change and includes a mostly complete checklist. All critical requirements appear addressed: algorithm added, documentation included, compliance with naming conventions and type hints confirmed, and doctests verified.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch feat/subarrays-fixed-bounds

📜 Recent review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 7052e00 and b497e84.

📒 Files selected for processing (2)
  • datastructures/arrays/subarrays_with_fixed_bounds/README.md (1 hunks)
  • datastructures/arrays/subarrays_with_fixed_bounds/__init__.py (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • datastructures/arrays/subarrays_with_fixed_bounds/README.md
🔇 Additional comments (2)
datastructures/arrays/subarrays_with_fixed_bounds/__init__.py (2)

29-31: LGTM! Edge case handling is appropriate.

Correctly handles empty arrays and invalid bounds (min_k > max_k) while allowing the valid case of min_k == max_k.


33-44: LGTM! Algorithm logic is correct and efficient.

The sliding window approach correctly counts subarrays by tracking the last positions of min_k, max_k, and invalid elements. The calculation on Line 44 accurately determines the number of valid starting positions for subarrays ending at each index. The implementation is efficient with O(n) time complexity and O(1) space complexity.


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: 1

🧹 Nitpick comments (3)
datastructures/arrays/subarrays_with_fixed_bounds/README.md (1)

12-22: Minor formatting inconsistency.

Line 15 has ≤10^3 while line 14 has ≤ 10^3 (with space). Consider adding a space for consistency: ≤ 10^3.

-- 2 ≤ nums.length ≤ 10^3 -- 1 ≤ nums[i], minK, maxK ≤10^3 +- 2 ≤ nums.length ≤ 10^3 +- 1 ≤ nums[i], minK, maxK ≤ 10^3
datastructures/arrays/subarrays_with_fixed_bounds/test_subarrays_with_fixed_bounds.py (1)

5-56: Good test coverage with room for improvement.

The test suite covers key scenarios including edge cases (equal bounds, no matches, mixed values). However, test names like test_1, test_2 are not very descriptive. Consider more meaningful names.

For better readability, consider renaming tests to describe what they test:

def test_single_valid_subarray_with_invalid_elements(self): # test_1 logic def test_multiple_overlapping_valid_subarrays(self): # test_2 logic def test_all_elements_equal_to_bounds(self): # test_3 logic def test_no_elements_match_bounds(self): # test_4 logic
datastructures/arrays/subarrays_with_fixed_bounds/__init__.py (1)

18-31: Efficient sliding window implementation.

The algorithm correctly uses a single-pass approach to track the last positions of min_k, max_k, and invalid elements, computing valid subarrays in O(n) time and O(1) space. The logic at line 29 correctly calculates the number of valid subarrays ending at each position.

Consider adding input validation for edge cases, though the current implementation handles them implicitly:

def count_subarrays(nums: List[int], min_k: int, max_k: int) -> int: """...""" if not nums or min_k > max_k: return 0 last_min, last_max, last_invalid = -1, -1, -1 count = 0 # ... rest of implementation
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between eb9734c and 7052e00.

⛔ Files ignored due to path filters (4)
  • datastructures/arrays/subarrays_with_fixed_bounds/images/subarrays_with_fixed_bounds_example_four.png is excluded by !**/*.png
  • datastructures/arrays/subarrays_with_fixed_bounds/images/subarrays_with_fixed_bounds_example_one.png is excluded by !**/*.png
  • datastructures/arrays/subarrays_with_fixed_bounds/images/subarrays_with_fixed_bounds_example_three.png is excluded by !**/*.png
  • datastructures/arrays/subarrays_with_fixed_bounds/images/subarrays_with_fixed_bounds_example_two.png is excluded by !**/*.png
📒 Files selected for processing (4)
  • DIRECTORY.md (1 hunks)
  • datastructures/arrays/subarrays_with_fixed_bounds/README.md (1 hunks)
  • datastructures/arrays/subarrays_with_fixed_bounds/__init__.py (1 hunks)
  • datastructures/arrays/subarrays_with_fixed_bounds/test_subarrays_with_fixed_bounds.py (1 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
datastructures/arrays/subarrays_with_fixed_bounds/test_subarrays_with_fixed_bounds.py (1)
datastructures/arrays/subarrays_with_fixed_bounds/__init__.py (1)
  • count_subarrays (4-31)
🪛 markdownlint-cli2 (0.18.1)
DIRECTORY.md

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

(MD007, ul-indent)


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

(MD007, ul-indent)

🔇 Additional comments (4)
DIRECTORY.md (1)

199-200: LGTM - Index entry added correctly.

The new subsection is properly placed under "Non Overlapping Intervals" and follows the existing file structure. The static analysis warning about indentation (MD007) reflects a pre-existing pattern in this file rather than an issue introduced by this PR.

datastructures/arrays/subarrays_with_fixed_bounds/README.md (1)

1-11: LGTM - Clear problem description.

The documentation provides a clear explanation of the fixed-bound subarray problem with well-defined conditions.

datastructures/arrays/subarrays_with_fixed_bounds/test_subarrays_with_fixed_bounds.py (1)

1-4: LGTM - Clean import structure.

The imports are minimal and appropriate for the test module.

datastructures/arrays/subarrays_with_fixed_bounds/__init__.py (1)

1-2: LGTM - Minimal necessary imports.

Clean import of typing for type hints.

@BrianLusina BrianLusina merged commit 1005257 into main Nov 12, 2025
6 of 8 checks passed
@BrianLusina BrianLusina deleted the feat/subarrays-fixed-bounds branch November 12, 2025 06:30
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

2 participants