🐍 Python for Everyone

← Back to practice bank

Chapter 28 Coding Challenges 45-60 min, auto-graded

Coding Challenges: Git Fundamentals

8 HackerRank-style challenges, auto-graded right in your browser — no local install needed, standard library only. This chapter teaches git itself, not Python syntax, so every challenge is a Python function that simulates or reasons about real git behavior — predicting what git log, git merge, or git rebase would actually do, given a history represented as plain data. 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, and no real git is installed or executed. Keep the function name in the stub exactly as given, since the grader calls it by name. Every expected value below was checked against real git's actual behavior in a scratch repository before being written into these tests — so a genuinely correct git mental model will pass every case.

1 Is a Fast-Forward Possible? Easy

Write a function is_fast_forward_merge(target_commits, source_commits). Both arguments are lists of commit ids representing a branch's full history from the root commit to its tip, in order. A fast-forward merge of source_commits into target_commits is possible only when target_commits is an exact, in-order prefix of source_commits (the target branch hasn't diverged -- every one of its commits also appears, in the same order, at the start of the source branch's history). Return True or False.

Constraints: both arguments are non-empty lists of strings.
Examples:
is_fast_forward_merge(["c1"], ["c1", "c2"]) → True
is_fast_forward_merge(["c1", "c3"], ["c1", "c2"]) → False
challenge_1.py
Show Solution
solution_1.py
def is_fast_forward_merge(target_commits, source_commits):
    n = len(target_commits)
    if n > len(source_commits):
        return False
    return list(source_commits[:n]) == list(target_commits)

2 Does the Merge Create a New Commit? Easy

Write a function merge_produces_new_commit(is_fast_forward). A fast-forward merge just moves the target branch's pointer forward -- no new commit object is created. A three-way merge (when the branches have diverged) always creates a new merge commit. Given whether the merge is a fast-forward, return True if it produces a new commit, else False.

Constraints: is_fast_forward is a boolean.
Examples:
merge_produces_new_commit(False) → True
merge_produces_new_commit(True) → False
challenge_2.py
Show Solution
solution_2.py
def merge_produces_new_commit(is_fast_forward):
    return not is_fast_forward

3 Build a Conflict Marker Block Easy

Write a function build_conflict_block(current_branch_line, incoming_branch_line, incoming_branch_name) that returns the 5 lines git would show for a single-line merge conflict, as a list of strings, in git's real order: the <<<<<<< HEAD marker, the current branch's (HEAD's) content, a ======= divider, the incoming branch's content, and a >>>>>>> <branch name> marker.

Constraints: all three arguments are strings.
Examples:
build_conflict_block("MAIN-CHANGE", "FEATURE-CHANGE", "feature") →
["<<<<<<< HEAD", "MAIN-CHANGE", "=======", "FEATURE-CHANGE", ">>>>>>> feature"]
challenge_3.py
Show Solution
solution_3.py
def build_conflict_block(current_branch_line, incoming_branch_line, incoming_branch_name):
    return [
        "<<<<<<< HEAD",
        current_branch_line,
        "=======",
        incoming_branch_line,
        ">>>>>>> " + incoming_branch_name,
    ]

4 What's Staged but Not Committed? Easy

Write a function staged_but_not_committed(added_files, committed_files) that returns the list of filenames that have been git add-ed but not yet included in a commit -- i.e. the filenames present in added_files but not in committed_files, preserving added_files's order.

Constraints: both arguments are lists of strings; added_files may contain names not present in committed_files and vice versa.
Examples:
staged_but_not_committed(["a.py", "b.py"], ["a.py"]) → ["b.py"]
challenge_4.py
Show Solution
solution_4.py
def staged_but_not_committed(added_files, committed_files):
    return [f for f in added_files if f not in committed_files]

5 Pick the Right Git Command Medium

Write a function pick_git_command(scenario) that returns the git command (as a plain string) that matches a described scenario. Support exactly these scenario strings, mapped to these commands: "see what changed but not staged yet" → "git diff"; "see what changed and is staged" → "git diff --staged"; "undo a commit that has already been pushed and shared" → "git revert"; "temporarily set aside uncommitted changes" → "git stash"; "see history of commits" → "git log"; "create and switch to a new branch in one step" → "git checkout -b". For any scenario string not in this list, return "unknown scenario".

Constraints: scenario is a string, matched exactly.
Examples:
pick_git_command("see history of commits") → "git log"
challenge_5.py
Show Solution
solution_5.py
def pick_git_command(scenario):
    mapping = {
        "see what changed but not staged yet": "git diff",
        "see what changed and is staged": "git diff --staged",
        "undo a commit that has already been pushed and shared": "git revert",
        "temporarily set aside uncommitted changes": "git stash",
        "see history of commits": "git log",
        "create and switch to a new branch in one step": "git checkout -b",
    }
    return mapping.get(scenario, "unknown scenario")

6 Commit Count After a Rebase Medium

Write a function commit_count_after_rebase(target_commit_count, source_new_commit_count). When you rebase a feature branch onto a target branch, git replays only the feature branch's commits that aren't already on the target -- one at a time -- on top of the target branch's current tip, producing a single linear history. Given how many commits the target branch has, and how many NEW commits the feature branch added since it diverged, return the total number of commits in the resulting linear history after the rebase.

Constraints: both arguments are non-negative ints.
Examples:
commit_count_after_rebase(2, 3) → 5
challenge_6.py
Show Solution
solution_6.py
def commit_count_after_rebase(target_commit_count, source_new_commit_count):
    return target_commit_count + source_new_commit_count

7 The Safe Undo Choice Medium

Write a function safe_undo_choice(already_pushed_and_shared). When a commit is still local and nobody else has it, git reset is a fine way to undo it. Once a commit has already been pushed and other people have pulled it, rewriting history with reset breaks everyone else's copy -- git revert (which adds a new commit instead of erasing history) is the safe choice instead. Return the string "revert" or "reset", whichever is safe for the given situation.

Constraints: already_pushed_and_shared is a boolean.
Examples:
safe_undo_choice(True) → "revert"
safe_undo_choice(False) → "reset"
challenge_7.py
Show Solution
solution_7.py
def safe_undo_choice(already_pushed_and_shared):
    return "revert" if already_pushed_and_shared else "reset"

8 Can the Reflog Recover It? Medium

Write a function can_recover_with_reflog(reflog_has_entry, garbage_collected). After a git reset --hard moves a branch backward, the commits it moved away from stop showing in git log but are NOT deleted right away -- they're still reachable through git reflog, and can be recovered with git reset --hard <reflog-hash>, as long as two things are both true: the reflog still has an entry pointing at them, and git's garbage collector hasn't already cleaned them up. Return True only when recovery is genuinely still possible.

Constraints: both arguments are booleans.
Examples:
can_recover_with_reflog(True, False) → True
can_recover_with_reflog(True, True) → False
challenge_8.py
Show Solution
solution_8.py
def can_recover_with_reflog(reflog_has_entry, garbage_collected):
    return reflog_has_entry and not garbage_collected
← Practice Bank Interview Questions →