feat(algorithms, k-way-merge): kth smallest element in sorted matrix#192
feat(algorithms, k-way-merge): kth smallest element in sorted matrix#192BrianLusina merged 2 commits intomainfrom
Conversation
📝 WalkthroughWalkthroughNew k-way merge algorithm added for computing the kth smallest element in a sorted matrix using heap-based approaches. Includes two implementations, comprehensive documentation, parameterised test coverage, and a minor docstring correction in an existing utility function. Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Suggested labels
Poem
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
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. Comment |
There was a problem hiding this comment.
Actionable comments posted: 4
🧹 Nitpick comments (1)
algorithms/k_way_merge/kth_smallest_element_in_matrix/test_kth_smallest_element_in_matrix.py (1)
33-42: Renamematrxtomatrixfor readability.Line 34 and Line 41 use a misspelt parameter name. It works, but correcting it improves clarity when scanning tests.
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@algorithms/k_way_merge/kth_smallest_element_in_matrix/test_kth_smallest_element_in_matrix.py` around lines 33 - 42, Rename the misspelled parameter `matrx` to `matrix` in the test functions so names are clear: update the function signatures for test_kth_smallest_in_matrix_1 and test_kth_smallest_in_matrix_2 to accept `matrix: List[List[int]]` (instead of `matrx`) and replace all uses inside those functions (e.g., the call to kth_smallest_in_matrix_with_heap_1(matrx, k)) to use `matrix`, ensuring parameterized.expand still supplies the same arguments.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@algorithms/k_way_merge/kth_smallest_element_in_matrix/__init__.py`:
- Around line 6-13: Add one executable doctest to each public function's
docstring (e.g., kth_smallest_element_in_matrix and the other public function
referenced at lines 33-40): include a small example matrix, a k value, and the
expected integer result using the Python doctest format (">>> func(matrix, k)"
followed by the result on the next line). Insert the doctest inside the existing
triple-quoted docstring before the closing quotes, keep it concise (one example
per function), and ensure the example uses the exact function name as defined so
tests can run.
- Around line 5-72: Add shared input validation at the start of both
kth_smallest_in_matrix_with_heap_1 and kth_smallest_in_matrix_with_heap_2:
verify matrix is not empty, all rows are non-empty (or treat total_elements =
sum(len(row) for row in matrix) > 0), k is a positive integer, and k <=
total_elements; if any check fails raise ValueError with a clear message. In
kth_smallest_in_matrix_with_heap_1 validate before building min_heap so we never
index out of range, and in kth_smallest_in_matrix_with_heap_2 validate before
using row_count/min_numbers (this prevents returning the default 0 when
min_numbers is empty); use the existing symbols matrix, k, row_count,
min_heap/min_numbers to locate where to add the checks.
In `@algorithms/k_way_merge/kth_smallest_element_in_matrix/README.md`:
- Around line 90-92: The README's space complexity line overstates memory:
update the "Space Complexity" section of kth_smallest_element_in_matrix to state
that the min-heap uses O(min(row_count, k)) space (or O(min(m, k))) rather than
O(n), and clarify briefly that the heap holds at most one entry per row (up to k
entries) in the heap-based approach (min-heap).
In `@datastructures/trees/binary/search_tree/bst_utils.py`:
- Line 35: In the bst_utils.py docstring there is a double space before "Kth"
("Finds the Kth smallest element in a binary search tree."); update that string
to a single space ("Finds the Kth smallest element in a binary search tree.") —
locate the docstring containing that exact sentence (e.g., in the function or
module that declares the BST utility like find_kth_smallest or similar) and
normalize the spacing so the documentation reads with a single space.
---
Nitpick comments:
In
`@algorithms/k_way_merge/kth_smallest_element_in_matrix/test_kth_smallest_element_in_matrix.py`:
- Around line 33-42: Rename the misspelled parameter `matrx` to `matrix` in the
test functions so names are clear: update the function signatures for
test_kth_smallest_in_matrix_1 and test_kth_smallest_in_matrix_2 to accept
`matrix: List[List[int]]` (instead of `matrx`) and replace all uses inside those
functions (e.g., the call to kth_smallest_in_matrix_with_heap_1(matrx, k)) to
use `matrix`, ensuring parameterized.expand still supplies the same arguments.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 68068257-b8ab-4584-a67c-3393ade4b9f6
⛔ Files ignored due to path filters (11)
algorithms/k_way_merge/kth_smallest_element_in_matrix/images/examples/kth_smallest_element_in_matrix_example_1.pngis excluded by!**/*.pngalgorithms/k_way_merge/kth_smallest_element_in_matrix/images/examples/kth_smallest_element_in_matrix_example_2.pngis excluded by!**/*.pngalgorithms/k_way_merge/kth_smallest_element_in_matrix/images/examples/kth_smallest_element_in_matrix_example_3.pngis excluded by!**/*.pngalgorithms/k_way_merge/kth_smallest_element_in_matrix/images/solutions/kth_smallest_element_in_matrix_solution_1.pngis excluded by!**/*.pngalgorithms/k_way_merge/kth_smallest_element_in_matrix/images/solutions/kth_smallest_element_in_matrix_solution_2.pngis excluded by!**/*.pngalgorithms/k_way_merge/kth_smallest_element_in_matrix/images/solutions/kth_smallest_element_in_matrix_solution_3.pngis excluded by!**/*.pngalgorithms/k_way_merge/kth_smallest_element_in_matrix/images/solutions/kth_smallest_element_in_matrix_solution_4.pngis excluded by!**/*.pngalgorithms/k_way_merge/kth_smallest_element_in_matrix/images/solutions/kth_smallest_element_in_matrix_solution_5.pngis excluded by!**/*.pngalgorithms/k_way_merge/kth_smallest_element_in_matrix/images/solutions/kth_smallest_element_in_matrix_solution_6.pngis excluded by!**/*.pngalgorithms/k_way_merge/kth_smallest_element_in_matrix/images/solutions/kth_smallest_element_in_matrix_solution_7.pngis excluded by!**/*.pngalgorithms/k_way_merge/kth_smallest_element_in_matrix/images/solutions/kth_smallest_element_in_matrix_solution_8.pngis excluded by!**/*.png
📒 Files selected for processing (6)
DIRECTORY.mdalgorithms/k_way_merge/__init__.pyalgorithms/k_way_merge/kth_smallest_element_in_matrix/README.mdalgorithms/k_way_merge/kth_smallest_element_in_matrix/__init__.pyalgorithms/k_way_merge/kth_smallest_element_in_matrix/test_kth_smallest_element_in_matrix.pydatastructures/trees/binary/search_tree/bst_utils.py
Describe your change:
Shows algorithms in the K-Way Merge Pattern to solve the problem of finding the kth smallest element in a sorted matrix.
Checklist:
Fixes: #{$ISSUE_NO}.Summary by CodeRabbit
Release Notes
New Features
Documentation
Tests