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.
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".
scenario is a string.
remote_command_for_scenario("download_without_merging") → "git fetch origin"
Show Solution
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"iflocal_aheadis 0 andremote_aheadis greater than 0"nothing_to_pull_push_instead"iflocal_aheadis greater than 0 andremote_aheadis 0"merge_required"if both are greater than 0 (the histories have diverged)
git status -sb's real [ahead X, behind Y] reporting, verified against a live bare-repo simulation.
pull_outcome(0, 3) → "fast_forward"pull_outcome(2, 3) → "merge_required"
Show Solution
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.
approvals/changes_requested are non-negative ints; ci_status is a string; has_conflicts is a bool.
is_pr_mergeable(1, 0, "passing", False) → Trueis_pr_mergeable(1, 0, "passing", True) → False
Show Solution
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).
has_write_access is a bool.
fork_or_clone(False) → "fork"
Show Solution
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").
review_state is a string.
classify_review("CHANGES_REQUESTED") → "blocked"
Show Solution
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"ifcommits_behind == 0"fetch_upstream_rebase_feature_branch_push"ifcommits_behind > 0andhas_open_prisTrue(rebasing keeps your PR's commits on top instead of discarding them)"fetch_upstream_merge_or_reset_default_branch"ifcommits_behind > 0andhas_open_prisFalse(no PR in flight, so simply catching up your default branch is enough)
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.
safe_stale_fork_update(40, True) → "fetch_upstream_rebase_feature_branch_push"
Show Solution
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).
title is a string; the other three arguments are bools.
issue_quality_score("Login fails", True, True, False) → 3issue_quality_score("", False, False, False) → 0
Show Solution
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".
current_step is always one of the nine valid step strings above.
next_workflow_step("pr_opened") → "reviewed"next_workflow_step("merged") → "done"
Show Solution
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]