binary-gap: infinite loop on negative input

Open Beginner friendly
#1 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
1/5
Estimated time
Under an hour
Newbie friendliness
78/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Quiet
Tech stack
python
Domain
backend

Research direction

Start by reading binary-gap/solution.py and exercise binary_gap with a negative input to confirm the non-terminating behavior. Add the requested guard or early return, then verify that negative inputs terminate and the existing positive-input behavior remains unchanged.

Written by the indexing model from the issue text.

Description

bug

`binary_gap` causes an infinite loop when passed a negative integer. Python's arbitrary-precision right-shift of a negative number never reaches `0`, so the while loop never terminates.

File: `binary-gap/solution.py`

Impact: Latent bug masked by LeetCode's constraint `1 <= n <= 10^9`. Would hang if constraints were relaxed to include negative integers.

Suggested fix: Add an early return or guard for `n <= 0`.

Dominant language
Python
Stars
0
Forks
0
PR merge metrics
No merged PRs in 30d

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from benthomasson/leetcode-implementations

All issues in benthomasson/leetcode-implementations

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.