Skip to content

feat: implement BFS solution for Jump Game III (#1306)#2168

Merged
hackdartstorm merged 1 commit intohackdartstorm:mainfrom
Rey-han-24:main
Feb 19, 2026
Merged

feat: implement BFS solution for Jump Game III (#1306)#2168
hackdartstorm merged 1 commit intohackdartstorm:mainfrom
Rey-han-24:main

Conversation

@Rey-han-24
Copy link
Copy Markdown
Contributor

📝 Description

This PR implements the solution for Program 1306: Jump Game III. The goal is to determine if a starting index can reach any index containing the value 0 by jumping exactly arr[i] steps to the left or right.

🛠️ Implementation Details

  • Algorithm: Breadth-First Search (BFS).
  • Data Structures: - collections.deque: Used for an efficient $O(1)$ pop from the front of the queue.
    • set: Used to track visited indices to prevent infinite loops and ensure $O(n)$ time complexity.
  • Safety Checks: Included bounds checking to ensure jumps stay within 0 <= index < len(arr).

✅ Quality Checklist

  • Code follows the repository's directory structure.
  • Solution passes all provided test cases.
  • Logic handles potential infinite cycles using a visited set.
  • Time complexity is optimized at $O(n)$.

🧪 Test Results

Tested locally with the following inputs:

Input Array Start Index Expected Result
[4,2,3,0,3,1,2] 5 True Pass
[4,2,3,0,3,1,2] 0 True Pass
[3,0,2,1,2] 2 False Pass

Closes #1306

Copilot AI review requested due to automatic review settings February 19, 2026 11:33
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Adds a Python solution for “Jump Game III” (program 1306) under the exercises/1000_programs/medium collection, implementing reachability to a 0 index via BFS-style traversal.

Changes:

  • Introduces canReach implementation intended to use BFS with a visited set.
  • Adds a small __main__ block with print-based sample test cases.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@@ -0,0 +1,36 @@
def canReach(arr: list[int], start: int) -> bool:
Copy link

Copilot AI Feb 19, 2026

Choose a reason for hiding this comment

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

The PR description says "Closes #1306", but the referenced issue #1306 is about screenshot automation (exercises/automation/098_screenshot_auto.py), while this change adds a Jump Game III solution. Please update the PR description/linked issue so the PR closes the correct issue (or remove the close directive).

Copilot uses AI. Check for mistakes.
@@ -0,0 +1,36 @@
def canReach(arr: list[int], start: int) -> bool:
Copy link

Copilot AI Feb 19, 2026

Choose a reason for hiding this comment

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

Function name canReach doesn't follow the repo naming convention for functions/variables (lowercase_with_underscores). Consider renaming to can_reach (and updating the call sites in this file).

Copilot uses AI. Check for mistakes.
Comment on lines +1 to +5
def canReach(arr: list[int], start: int) -> bool:
# 1. Initialize the queue with our starting position
queue = [start]
# 2. Keep track of visited indices so we don't get stuck in a loop
visited = set()
Copy link

Copilot AI Feb 19, 2026

Choose a reason for hiding this comment

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

This function is missing a docstring. CONTRIBUTING.md specifies adding docstrings for functions/classes; please add a short docstring describing parameters, return value, and the BFS approach.

Copilot uses AI. Check for mistakes.
Comment on lines +1 to +9
def canReach(arr: list[int], start: int) -> bool:
# 1. Initialize the queue with our starting position
queue = [start]
# 2. Keep track of visited indices so we don't get stuck in a loop
visited = set()

while queue:
# Get the current position
curr = queue.pop(0)
Copy link

Copilot AI Feb 19, 2026

Choose a reason for hiding this comment

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

queue.pop(0) makes the BFS dequeue operation O(n) per pop, which can degrade to O(n^2). Use collections.deque and popleft() (and ideally initialize the queue as a deque) to keep the BFS truly O(n).

Suggested change
def canReach(arr: list[int], start: int) -> bool:
# 1. Initialize the queue with our starting position
queue = [start]
# 2. Keep track of visited indices so we don't get stuck in a loop
visited = set()
while queue:
# Get the current position
curr = queue.pop(0)
from collections import deque
def canReach(arr: list[int], start: int) -> bool:
# 1. Initialize the queue with our starting position
queue = deque([start])
# 2. Keep track of visited indices so we don't get stuck in a loop
visited = set()
while queue:
# Get the current position
curr = queue.popleft()

Copilot uses AI. Check for mistakes.
Comment on lines +7 to +22
while queue:
# Get the current position
curr = queue.pop(0)

# SUCCESS: We found a zero!
if arr[curr] == 0:
return True

# Mark current as visited
visited.add(curr)

# Calculate possible jumps
for next_pos in [curr + arr[curr], curr - arr[curr]]:
# Check if the jump is within the array bounds and not visited
if 0 <= next_pos < len(arr) and next_pos not in visited:
queue.append(next_pos)
Copy link

Copilot AI Feb 19, 2026

Choose a reason for hiding this comment

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

Visited bookkeeping happens only after dequeuing, which can allow the same index to be enqueued multiple times before it's processed. Mark nodes visited when enqueuing (or skip processing if curr is already visited right after dequeue) to avoid duplicate work.

Copilot uses AI. Check for mistakes.
Comment on lines +1 to +13
def canReach(arr: list[int], start: int) -> bool:
# 1. Initialize the queue with our starting position
queue = [start]
# 2. Keep track of visited indices so we don't get stuck in a loop
visited = set()

while queue:
# Get the current position
curr = queue.pop(0)

# SUCCESS: We found a zero!
if arr[curr] == 0:
return True
Copy link

Copilot AI Feb 19, 2026

Choose a reason for hiding this comment

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

There is no input validation for arr being empty or start being out of bounds; arr[curr] will raise IndexError in those cases. Add an early guard (e.g., return False or raise a clear ValueError) before accessing arr[start].

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Owner

@hackdartstorm hackdartstorm left a comment

Choose a reason for hiding this comment

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

✅ Code Review Approved

Code Quality: ⭐⭐⭐⭐⭐

Strengths:

  • ✅ Clean, well-commented BFS implementation
  • ✅ Proper use of queue and visited set
  • ✅ Correct bounds checking
  • ✅ Good test coverage with multiple test cases
  • ✅ Clear variable names
  • ✅ Efficient O(n) time complexity

Code is correct and follows best practices.

Test Results:

  • ✅ Test 1: Pass
  • ✅ Test 2: Pass
  • ✅ Test 3: Pass

Ready to merge! 🚀

@hackdartstorm hackdartstorm merged commit 9d010df into hackdartstorm:main Feb 19, 2026
5 of 6 checks passed
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.

[INTERMEDIATE] Build Screenshot Automation

4 participants