🐍 Python for Everyone

← Back to practice bank

Chapter 29 Coding Challenges 40-55 min, auto-graded

Coding Challenges: GitHub & Collaboration

8 HackerRank-style challenges, auto-graded right in your browser — no local install needed, standard library only. Write your function in the code box, then click Run Tests to check it against a set of test cases (including edge cases). Stuck? Every challenge has a Show Solution reveal at the bottom.

How grading works Your code runs entirely in your browser using Pyodide (Python compiled to WebAssembly) — nothing is uploaded anywhere. Keep the function name in the stub exactly as given, since the grader calls it by name. A real browser can't run actual git/GitHub operations, so every challenge here reasons about or simulates a GitHub collaboration scenario as plain function logic — but the underlying git behavior each one encodes (fast-forward vs. merge, remote commands, ahead/behind counts) was checked against a real local bare-repo simulation before being written into these test cases.

1 Pick the Right Remote Command Easy

Write a function remote_command_for_scenario(scenario) that takes a scenario name (a string) and returns the exact git command (as a string) that solves it. Use this mapping:

  • "add_upstream" → "git remote add upstream <url>"
  • "list_remotes_verbose" → "git remote -v"
  • "download_without_merging" → "git fetch origin"
  • "download_and_merge" → "git pull origin main"
  • "first_push_new_branch" → "git push -u origin <branch>"

For any other scenario string, return "unknown scenario".

Constraints: scenario is a string.
Examples:
remote_command_for_scenario("download_without_merging") → "git fetch origin"
challenge_1.py
Show Solution
solution_1.py
def remote_command_for_scenario(scenario):
    mapping = {
        "add_upstream": "git remote add upstream <url>",
        "list_remotes_verbose": "git remote -v",
        "download_without_merging": "git fetch origin",
        "download_and_merge": "git pull origin main",
        "first_push_new_branch": "git push -u origin <branch>",
    }
    return mapping.get(scenario, "unknown scenario")

2 Fast-Forward or Merge Required? Medium

Write a function pull_outcome(local_ahead, remote_ahead) that, given how many commits your local branch is ahead of the common ancestor (local_ahead) and how many commits the remote-tracking branch is ahead of the same ancestor (remote_ahead), returns one of four strings describing what git pull would do:

  • "up_to_date" if both are 0
  • "fast_forward" if local_ahead is 0 and remote_ahead is greater than 0
  • "nothing_to_pull_push_instead" if local_ahead is greater than 0 and remote_ahead is 0
  • "merge_required" if both are greater than 0 (the histories have diverged)
Constraints: both arguments are non-negative ints. This mirrors git status -sb's real [ahead X, behind Y] reporting, verified against a live bare-repo simulation.
Examples:
pull_outcome(0, 3) → "fast_forward"
pull_outcome(2, 3) → "merge_required"
challenge_2.py
Show Solution
solution_2.py
def pull_outcome(local_ahead, remote_ahead):
    if local_ahead == 0 and remote_ahead == 0:
        return "up_to_date"
    if local_ahead == 0 and remote_ahead > 0:
        return "fast_forward"
    if local_ahead > 0 and remote_ahead == 0:
        return "nothing_to_pull_push_instead"
    return "merge_required"

3 Is This PR Mergeable? Medium

Write a function is_pr_mergeable(approvals, changes_requested, ci_status, has_conflicts) that returns True only if all of the following hold: approvals is at least 1, changes_requested is exactly 0, ci_status is the string "passing", and has_conflicts is False. Otherwise return False.

Constraints: approvals/changes_requested are non-negative ints; ci_status is a string; has_conflicts is a bool.
Examples:
is_pr_mergeable(1, 0, "passing", False) → True
is_pr_mergeable(1, 0, "passing", True) → False
challenge_3.py
Show Solution
solution_3.py
def is_pr_mergeable(approvals, changes_requested, ci_status, has_conflicts):
    return (
        approvals >= 1
        and changes_requested == 0
        and ci_status == "passing"
        and not has_conflicts
    )

4 Fork or Clone? Easy

Write a function fork_or_clone(has_write_access) that returns "clone" if has_write_access is True (you can clone the repo directly and push branches), or "fork" if False (you need your own fork to push to, then open a PR back to the original).

Constraints: has_write_access is a bool.
Examples:
fork_or_clone(False) → "fork"
challenge_4.py
Show Solution
solution_4.py
def fork_or_clone(has_write_access):
    return "clone" if has_write_access else "fork"

5 Classify a Review State Easy

Write a function classify_review(review_state) that takes a GitHub review state string and returns "merge_allowed" if review_state == "APPROVED", "blocked" if review_state == "CHANGES_REQUESTED", and "informational" for any other value (e.g. "COMMENTED" or "DISMISSED").

Constraints: review_state is a string.
Examples:
classify_review("CHANGES_REQUESTED") → "blocked"
challenge_5.py
Show Solution
solution_5.py
def classify_review(review_state):
    if review_state == "APPROVED":
        return "merge_allowed"
    if review_state == "CHANGES_REQUESTED":
        return "blocked"
    return "informational"

6 Update a Stale Fork Safely Medium

Write a function safe_stale_fork_update(commits_behind, has_open_pr) for this real scenario: your fork's branch has fallen commits_behind commits behind the upstream default branch. Return:

  • "up_to_date" if commits_behind == 0
  • "fetch_upstream_rebase_feature_branch_push" if commits_behind > 0 and has_open_pr is True (rebasing keeps your PR's commits on top instead of discarding them)
  • "fetch_upstream_merge_or_reset_default_branch" if commits_behind > 0 and has_open_pr is False (no PR in flight, so simply catching up your default branch is enough)
Constraints: commits_behind is a non-negative int; has_open_pr is a bool. This flow (fetch upstream, then rebase the feature branch, keeping the fork's own PR commits) was verified against a real local bare-repo simulation of a stale fork before being written here.
Examples:
safe_stale_fork_update(40, True) → "fetch_upstream_rebase_feature_branch_push"
challenge_6.py
Show Solution
solution_6.py
def safe_stale_fork_update(commits_behind, has_open_pr):
    if commits_behind == 0:
        return "up_to_date"
    if has_open_pr:
        return "fetch_upstream_rebase_feature_branch_push"
    return "fetch_upstream_merge_or_reset_default_branch"

7 Score an Issue's Quality Medium

Write a function issue_quality_score(title, has_repro_steps, has_expected_behavior, has_environment_info) that scores a bug report out of 4: if title is empty (falsy), return 0 immediately. Otherwise start at 1 point for having a title, then add 1 point each for has_repro_steps, has_expected_behavior, and has_environment_info being True. Return the total (an int from 0 to 4).

Constraints: title is a string; the other three arguments are bools.
Examples:
issue_quality_score("Login fails", True, True, False) → 3
issue_quality_score("", False, False, False) → 0
challenge_7.py
Show Solution
solution_7.py
def issue_quality_score(title, has_repro_steps, has_expected_behavior, has_environment_info):
    if not title:
        return 0
    score = 1
    if has_repro_steps:
        score += 1
    if has_expected_behavior:
        score += 1
    if has_environment_info:
        score += 1
    return score

8 Next Step in the Collaboration Workflow Medium

Write a function next_workflow_step(current_step) that returns the next step in this fixed, real-world order: "issue_filed" → "forked" → "branched" → "committed" → "pushed" → "pr_opened" → "reviewed" → "approved" → "merged". If current_step is "merged" (the last step), return "done".

Constraints: current_step is always one of the nine valid step strings above.
Examples:
next_workflow_step("pr_opened") → "reviewed"
next_workflow_step("merged") → "done"
challenge_8.py
Show Solution
solution_8.py
def next_workflow_step(current_step):
    order = [
        "issue_filed", "forked", "branched", "committed", "pushed",
        "pr_opened", "reviewed", "approved", "merged",
    ]
    idx = order.index(current_step)
    if idx == len(order) - 1:
        return "done"
    return order[idx + 1]
← Practice Bank Interview Questions →