Skip to content

LeetCode #16: 3Sum Closest#134

Merged
WazedKhan merged 1 commit into
mainfrom
leetcode-16-three-sum-closest
Jun 22, 2026
Merged

LeetCode #16: 3Sum Closest#134
WazedKhan merged 1 commit into
mainfrom
leetcode-16-three-sum-closest

Conversation

@WazedKhan

@WazedKhan WazedKhan commented Jun 22, 2026

Copy link
Copy Markdown
Owner

Summary by CodeRabbit

  • New Features

    • Added solution implementation for the LeetCode "3Sum Closest" problem
  • Tests

    • Added parametrized test cases with multiple scenarios to validate the new solution

@coderabbitai

coderabbitai Bot commented Jun 22, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

Adds a new file LeetCode/medium/three_sum_closest_16.py implementing Solution.threeSumClosest using a sort-and-two-pointer approach. A corresponding parametrized pytest block is added to tests/test_leetcode_medium.py to verify correctness across multiple input cases.

Changes

3Sum Closest Solution

Layer / File(s) Summary
Implementation and tests
LeetCode/medium/three_sum_closest_16.py, tests/test_leetcode_medium.py
Solution.threeSumClosest sorts nums, iterates each index as the first element, and uses a two-pointer scan to find and track the three-number sum nearest to target. Tests verify the function against multiple (nums, target, expected) cases.

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~3 minutes

Poem

🐇 Hoppity-hop through the sorted array,
Two pointers dancing, they find their way.
Closest to target? I've got the knack!
Left pointer forward, right pointer back.
The rabbit solves sums without delay! 🥕

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'LeetCode #16: 3Sum Closest' clearly and specifically identifies the problem being solved, directly matching the main change of implementing the 3Sum Closest solution.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch leetcode-16-three-sum-closest

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.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🧹 Nitpick comments (1)
LeetCode/medium/three_sum_closest_16.py (1)

13-17: 🧹 Nitpick | 🔵 Trivial | ⚡ Quick win

Consider early return on exact match.

When three_sum == target, the algorithm has found the closest possible sum (distance = 0). Consider adding an early return for a performance optimization:

⚡ Suggested optimization
             while left < right:
                 three_sum = nums[index] + nums[left] + nums[right]
+                if three_sum == target:
+                    return target
                 if three_sum < target:
                     left += 1
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@LeetCode/medium/three_sum_closest_16.py` around lines 13 - 17, The current
logic in the three_sum closest calculation only checks if three_sum < target or
executes an else clause, but does not explicitly handle the case when three_sum
== target. Since an exact match represents the minimum possible distance (0),
add an early return condition before the existing if three_sum < target check to
return target immediately when three_sum equals target. This optimization avoids
unnecessary iterations when the optimal answer is already found.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@LeetCode/medium/three_sum_closest_16.py`:
- Around line 13-17: The current logic in the three_sum closest calculation only
checks if three_sum < target or executes an else clause, but does not explicitly
handle the case when three_sum == target. Since an exact match represents the
minimum possible distance (0), add an early return condition before the existing
if three_sum < target check to return target immediately when three_sum equals
target. This optimization avoids unnecessary iterations when the optimal answer
is already found.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 49c9f56f-d2df-4274-8649-e6530522c914

📥 Commits

Reviewing files that changed from the base of the PR and between 6fa9e51 and 6a8f3ce.

📒 Files selected for processing (2)
  • LeetCode/medium/three_sum_closest_16.py
  • tests/test_leetcode_medium.py

@WazedKhan WazedKhan merged commit 50e054a into main Jun 22, 2026
3 checks passed
@WazedKhan WazedKhan deleted the leetcode-16-three-sum-closest branch June 22, 2026 05:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant