Coding Challenges: CI/CD Pipelines
8 HackerRank-style challenges, auto-graded right in your browser — no local install needed, standard library only. This chapter teaches GitHub Actions (YAML-based CI/CD), not new Python syntax, so every challenge is a Python function that parses, validates, or reasons about real GitHub Actions behavior — predicting what a workflow file's triggers do, how many job runs a matrix strategy produces, or whether a set of required checks allows a merge — given the workflow 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.
yaml package,
since pyyaml isn't available in this browser sandbox. 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
GitHub Actions semantics before being written into these tests
— so a genuinely correct CI/CD mental model will pass every
case.
1 Does It Trigger on pull_request? Easy
Write a function triggers_on_pull_request(yaml_lines). yaml_lines is a list of strings, each one line of a workflow YAML file. The trigger appears either as a single line like "on: pull_request", or as a multi-line block where "on:" is followed by indented lines like " push:" and " pull_request:". Return True if the workflow triggers on pull_request in either form, else False.
yaml_lines is a non-empty list of strings; at most one "on:" section is present.
triggers_on_pull_request(["name: CI", "on: pull_request", "jobs:"]) → Truetriggers_on_pull_request(["name: CI", "on: push", "jobs:"]) → False
Show Solution
def triggers_on_pull_request(yaml_lines):
in_on_block = False
for line in yaml_lines:
if line.startswith("on:"):
rest = line.split(":", 1)[1].strip()
if rest == "pull_request":
return True
in_on_block = True
continue
if in_on_block:
stripped = line.strip()
if line.startswith((" ", "\t")):
if stripped in ("pull_request:", "pull_request"):
return True
continue
else:
in_on_block = False
return False
2 Matrix Job Count Easy
Write a function matrix_job_count(dimensions). dimensions is a dict where each key is a matrix dimension name (like "python-version" or "os") and each value is a list of options for that dimension. A matrix strategy runs the job once for every combination across all dimensions. Return the total number of job runs produced.
dimensions is a dict with 0 or more keys; every value is a non-empty list.
matrix_job_count({"python-version": ["3.9", "3.10", "3.11"]}) → 3matrix_job_count({"python-version": ["3.9", "3.10"], "os": ["ubuntu-latest", "windows-latest", "macos-latest"]}) → 6
Show Solution
def matrix_job_count(dimensions):
total = 1
for values in dimensions.values():
total *= len(values)
return total
3 Extract Job Names Easy
Write a function extract_job_names(yaml_lines) that returns the list of job names defined under the workflow's "jobs:" section, in order. Job names are the first indentation level under jobs: (e.g. " lint:" and " test:" both start a job at 2 spaces of indent); deeper-indented lines like runs-on or step lines are not job names.
yaml_lines contains exactly one "jobs:" line; job name lines end with ":" and are indented with spaces.
extract_job_names(["jobs:", " lint:", " runs-on: ubuntu-latest", " test:", " runs-on: ubuntu-latest"]) → ["lint", "test"]
Show Solution
def extract_job_names(yaml_lines):
names = []
in_jobs_block = False
jobs_indent = None
for line in yaml_lines:
if line.startswith("jobs:"):
in_jobs_block = True
continue
if in_jobs_block:
if not line.strip():
continue
indent = len(line) - len(line.lstrip(" "))
if jobs_indent is None and line.startswith(" ") and line.rstrip().endswith(":"):
jobs_indent = indent
if jobs_indent is not None and indent == jobs_indent and line.rstrip().endswith(":"):
names.append(line.strip().rstrip(":"))
elif jobs_indent is not None and indent < jobs_indent:
break
return names
4 Are All Required Checks Passed? Easy
Write a function all_required_checks_passed(required_checks, passed_checks) that returns True only if every check name in required_checks also appears in passed_checks — this is what GitHub actually checks before allowing a PR to merge with branch protection enabled.
passed_checks may contain extra checks not in required_checks.
all_required_checks_passed(["lint", "test"], ["lint", "test", "build"]) → Trueall_required_checks_passed(["lint", "test"], ["lint"]) → False
Show Solution
def all_required_checks_passed(required_checks, passed_checks):
return all(check in passed_checks for check in required_checks)
5 Find the Missing Checkout Step Medium
Write a function missing_checkout_step(step_names). Every job that runs commands against repo files needs an "actions/checkout" step, or the runner's workspace is empty. Given a list of step names/actions used in a job, return True if "actions/checkout" is NOT among them (a bug), else False.
step_names is a list of strings, possibly empty.
missing_checkout_step(["setup-python", "install", "test"]) → Truemissing_checkout_step(["actions/checkout", "setup-python", "test"]) → False
Show Solution
def missing_checkout_step(step_names):
return "actions/checkout" not in step_names
6 Should This Cache Be Reused? Medium
Write a function should_reuse_cache(current_key, available_cache_keys). GitHub Actions only restores a dependency cache when its exact key (typically built from the OS, Python version, and a hash of the lockfile) matches one already stored. Return True if current_key is present in available_cache_keys, else False.
current_key is a string; available_cache_keys is a list of strings.
should_reuse_cache("py3.11-pip-abc123", ["py3.10-pip-xyz", "py3.11-pip-abc123"]) → Trueshould_reuse_cache("py3.11-pip-abc123", ["py3.10-pip-xyz"]) → False
Show Solution
def should_reuse_cache(current_key, available_cache_keys):
return current_key in available_cache_keys
7 Is This a Valid Pipeline Stage Order? Medium
Write a function is_valid_stage_order(stages). A well-formed pipeline runs its stages in this canonical order: lint, type_check, test, security_scan, build, deploy_staging, deploy_production (a stage can be skipped, but present stages must appear in this relative order). Given a list of stage names in the order they actually run, return True if that order is consistent with the canonical order, else False.
stages is a list of strings, each one of the 7 canonical stage names; no duplicates.
is_valid_stage_order(["lint", "test", "deploy_staging", "deploy_production"]) → Trueis_valid_stage_order(["deploy_production", "lint", "test"]) → False
Show Solution
def is_valid_stage_order(stages):
order = ["lint", "type_check", "test", "security_scan", "build",
"deploy_staging", "deploy_production"]
positions = [order.index(s) for s in stages if s in order]
return positions == sorted(positions)
8 Pick the Right Trigger Medium
Write a function pick_trigger_for_scenario(scenario) that returns the GitHub Actions trigger keyword (as a plain string) matching a described scenario. Support exactly these mappings: "run on every push to any branch" → "push"; "run before merging a pull request" → "pull_request"; "run on a schedule every night" → "schedule"; "let a developer trigger it manually from the Actions tab" → "workflow_dispatch". For any scenario string not in this list, return "unknown scenario".
scenario is a string, matched exactly.
pick_trigger_for_scenario("run on a schedule every night") → "schedule"
Show Solution
def pick_trigger_for_scenario(scenario):
mapping = {
"run on every push to any branch": "push",
"run before merging a pull request": "pull_request",
"run on a schedule every night": "schedule",
"let a developer trigger it manually from the Actions tab": "workflow_dispatch",
}
return mapping.get(scenario, "unknown scenario")