Interview questions

Take home challenges interview challenges

53 challenges to build at home. Every member practises all of them free.

Samples

3 of 53 challenges.

DevOps Troubleshooting Challenge: The Broken CI/CD Pipeline

Architecture: The pipeline you are fixingA push runs the tests, builds the image and pushes it to Docker Hub. Four faults stop it along the way.
The pipeline you are fixing. A push runs the tests, builds the image and pushes it to Docker Hub. Four faults stop it along the way.
Describe this diagram
Parts
  • Developer: Pushes a commit to the repository.
  • status-api repository: The repository with the app, its tests, the Dockerfile and the workflow.
  • Unit tests: unittest checks the /health endpoint. The pipeline stops here if a test fails.
  • Docker build: Builds the image from the Dockerfile and tags it with your Docker Hub user.
  • Repository secrets: REGISTRY_USER and REGISTRY_TOKEN, used to log in to Docker Hub.
  • Docker Hub: Receives the image as REGISTRY_USER/status-api.
Connections
  • Developer to status-api repository: git push
  • status-api repository to Unit tests: starts the workflow
  • Unit tests to Docker build: when tests pass
  • Docker build to Docker Hub: push USER/status-api
  • Repository secrets to Docker build: registry login
Show the answer

Four faults stop the pipeline:

  1. Unit test: response.data is bytes, so comparing it with the string "Service is up" always fails. Compare with b"Service is up" or use response.get_data(as_text=True).
  2. Dockerfile: -no-cache-dir is not a pip option. It is --no-cache-dir.
  3. Build step: dokcer is a typo for docker.
  4. Push step: an image tagged status-api pushes to the official library namespace, which is refused. Tag it with your Docker Hub user, and document the two repository secrets the step needs.

The fixed files:

.github/workflows/ci.yml

name: ci
on:
  push:
    branches: [main]
  pull_request:

jobs:
  build:
    runs-on: ubuntu-latest
    env:
      IMAGE: ${{ secrets.REGISTRY_USER }}/status-api
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-python@v5
        with:
          python-version: "3.12"
          cache: pip

      - name: Test
        run: |
          pip install -r requirements.txt
          python -m unittest discover -s tests -t .

      - name: Build image
        run: docker build -t "$IMAGE:${{ github.sha }}" -t "$IMAGE:latest" .

      - name: Push image
        if: github.ref == 'refs/heads/main'
        run: |
          echo "${{ secrets.REGISTRY_TOKEN }}" | docker login -u "${{ secrets.REGISTRY_USER }}" --password-stdin
          docker push --all-tags "$IMAGE"

Dockerfile

FROM python:3.12-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
USER 10001
EXPOSE 8000
CMD ["python", "app.py"]

tests/test_app.py

import unittest

from app import app


class HealthTest(unittest.TestCase):
    def setUp(self):
        self.client = app.test_client()

    def test_health(self):
        response = self.client.get("/health")
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.get_data(as_text=True), "Service is up")

README.md

# status-api

A small Flask service. CI tests every push and pull request, and on `main` builds the image and pushes it to Docker Hub.

## Repository secrets

| Secret | Value |
| --- | --- |
| `REGISTRY_USER` | Your Docker Hub user name. The image is pushed as `REGISTRY_USER/status-api`. |
| `REGISTRY_TOKEN` | A Docker Hub access token with write access. Never your password. |

## Run locally

    pip install -r requirements.txt
    python -m unittest discover -s tests -t .
    python app.py

AWS Lambda/Terraform Troubleshooting

Architecture: The event store you are repairingTerraform packages and deploys the function. Each invoke stores the event as a JSON object in a private bucket.
The event store you are repairing. Terraform packages and deploys the function. Each invoke stores the event as a JSON object in a private bucket.
Describe this diagram
Parts
  • Terraform: Creates the bucket, the role and the function, and zips the code itself.
  • lambda.zip: The code package Terraform builds from lambda/ with archive_file.
  • store-events function: Python 3.12, handler handler.main, with DATA_BUCKET set.
  • Event bucket (private): Private, with every public access setting blocked.
  • IAM role: Basic execution for logs, and PutObject on events/ in this bucket only.
  • CloudWatch Logs: /aws/lambda/store-events, where errors show up first.
Connections
  • Terraform to lambda.zip: zips lambda/
  • lambda.zip to store-events function: deploys
  • store-events function to Event bucket (private): PutObject events/
  • IAM role to store-events function: assumed by
  • store-events function to CloudWatch Logs: logs
Show the answer

Six faults, found in the order terraform apply and a test invoke reveal them:

  1. acl on aws_s3_bucket: removed from the bucket resource in version 5 of the AWS provider. Leave the bucket private by default and add a public access block.
  2. Missing package: s3_key names a zip that was never uploaded, so the function cannot be created. Build the zip with the archive_file data source and deploy it directly, with source_code_hash so a code change redeploys.
  3. Handler name: the function is main, not handler, so the handler is handler.main.
  4. Runtime: python3.8 is deprecated for new functions. Use python3.12.
  5. Code: json is used but never imported, and DATA_BUCKET is never set on the function.
  6. Permissions: the role allows nothing, so the function cannot write logs or objects. Attach the basic execution policy and allow s3:PutObject on this bucket only.

Test it with aws lambda invoke --function-name store-events --payload '{"order":42}' --cli-binary-format raw-in-base64-out out.json, then list events/ in the bucket. A second terraform apply must show no changes.

The fixed files:

terraform/main.tf

terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 5.0"
    }
    archive = {
      source  = "hashicorp/archive"
      version = "~> 2.4"
    }
  }
}

provider "aws" {
  region = "eu-west-2"
}

resource "aws_s3_bucket" "events" {
  bucket = "supercooltech-event-store"
}

resource "aws_s3_bucket_public_access_block" "events" {
  bucket                  = aws_s3_bucket.events.id
  block_public_acls       = true
  block_public_policy     = true
  ignore_public_acls      = true
  restrict_public_buckets = true
}

resource "aws_iam_role" "lambda" {
  name = "event-store-lambda"
  assume_role_policy = jsonencode({
    Version = "2012-10-17"
    Statement = [{
      Effect    = "Allow"
      Action    = "sts:AssumeRole"
      Principal = { Service = "lambda.amazonaws.com" }
    }]
  })
}

resource "aws_iam_role_policy_attachment" "logs" {
  role       = aws_iam_role.lambda.name
  policy_arn = "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
}

resource "aws_iam_role_policy" "write_events" {
  name = "write-events"
  role = aws_iam_role.lambda.id
  policy = jsonencode({
    Version = "2012-10-17"
    Statement = [{
      Effect   = "Allow"
      Action   = "s3:PutObject"
      Resource = "${aws_s3_bucket.events.arn}/events/*"
    }]
  })
}

data "archive_file" "package" {
  type        = "zip"
  source_dir  = "${path.module}/../lambda"
  output_path = "${path.module}/build/lambda.zip"
}

resource "aws_lambda_function" "store" {
  function_name    = "store-events"
  role             = aws_iam_role.lambda.arn
  runtime          = "python3.12"
  handler          = "handler.main"
  filename         = data.archive_file.package.output_path
  source_code_hash = data.archive_file.package.output_base64sha256
  timeout          = 10

  environment {
    variables = { DATA_BUCKET = aws_s3_bucket.events.bucket }
  }

  depends_on = [aws_iam_role_policy_attachment.logs]
}

lambda/handler.py

import json
import os

import boto3

s3 = boto3.client("s3")
BUCKET = os.environ["DATA_BUCKET"]


def main(event, context):
    key = f"events/{context.aws_request_id}.json"
    s3.put_object(Bucket=BUCKET, Key=key, Body=json.dumps(event).encode(), ContentType="application/json")
    return {"statusCode": 200, "body": json.dumps({"stored": key})}

README.md

# event-store

Stores each event the function receives as a JSON object under `events/` in a private bucket.

## Deploy

    cd terraform
    terraform init
    terraform apply

Terraform zips `lambda/` itself, so there is nothing to upload by hand. Change the code and apply again to redeploy.

## Test

    aws lambda invoke --function-name store-events \
      --payload '{"order":42}' --cli-binary-format raw-in-base64-out out.json
    cat out.json
    aws s3 ls "s3://supercooltech-event-store/events/"

Logs are in CloudWatch under `/aws/lambda/store-events`.

Create Immutable AMIs with Packer

Architecture: Immutable web serversPacker bakes Nginx into an image. Terraform starts a server from the newest image, and a change means a new image, never a patched server.
Immutable web servers. Packer bakes Nginx into an image. Terraform starts a server from the newest image, and a change means a new image, never a patched server.
Describe this diagram
Parts
  • Packer: Builds from Ubuntu 24.04, installs Nginx and writes the page.
  • AMI web-nginx-*: Tagged Role=web-nginx with the build time.
  • Terraform: Finds the newest image by tag, so no AMI ID is copied by hand.
  • web-nginx instance: Serves the page from the image, metadata v2 only.
  • Visitors: curl http://<public ip> shows the build time.
Connections
  • Packer to AMI web-nginx-*: packer build
  • Terraform to AMI web-nginx-*: finds newest by tag
  • AMI web-nginx-* to web-nginx instance: boots from
  • Terraform to web-nginx instance: launches
  • Visitors to web-nginx instance: HTTP 80
Show the answer

Approach: Packer builds an Ubuntu 24.04 image with Nginx installed, enabled and serving a page that names the build. Terraform never takes a hard coded AMI ID: it looks up the newest image Packer built, by tag, and starts an instance from it in the default VPC with only port 80 open. To roll out a change, build a new image and apply again. The old instance is replaced, never patched in place.

Check it with curl "http://$(terraform output -raw public_ip)", which returns the page with the build time.

packer/web.pkr.hcl

packer {
  required_plugins {
    amazon = {
      source  = "github.com/hashicorp/amazon"
      version = "~> 1.3"
    }
  }
}

variable "region" {
  type    = string
  default = "eu-west-2"
}

locals {
  stamp = formatdate("YYYYMMDDhhmmss", timestamp())
}

source "amazon-ebs" "web" {
  region        = var.region
  instance_type = "t3.micro"
  ssh_username  = "ubuntu"
  ami_name      = "web-nginx-${local.stamp}"

  source_ami_filter {
    filters = {
      name                = "ubuntu/images/hvm-ssd-gp3/ubuntu-noble-24.04-amd64-server-*"
      virtualization-type = "hvm"
      root-device-type    = "ebs"
    }
    owners      = ["099720109477"]
    most_recent = true
  }

  tags = {
    Role  = "web-nginx"
    Built = local.stamp
  }
}

build {
  sources = ["source.amazon-ebs.web"]

  provisioner "shell" {
    environment_vars = ["BUILT=${local.stamp}"]
    inline = [
      "sudo apt-get update",
      "sudo DEBIAN_FRONTEND=noninteractive apt-get install -y nginx",
      "echo \"<h1>web-nginx</h1><p>Image built $BUILT</p>\" | sudo tee /var/www/html/index.html",
      "sudo systemctl enable nginx",
    ]
  }
}

terraform/main.tf

terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 5.0"
    }
  }
}

provider "aws" {
  region = var.region
}

variable "region" {
  type    = string
  default = "eu-west-2"
}

# The newest image Packer built, so no AMI ID is ever copied by hand.
data "aws_ami" "web" {
  owners      = ["self"]
  most_recent = true
  filter {
    name   = "tag:Role"
    values = ["web-nginx"]
  }
}

data "aws_vpc" "default" {
  default = true
}

resource "aws_security_group" "web" {
  name   = "web-nginx"
  vpc_id = data.aws_vpc.default.id

  ingress {
    from_port   = 80
    to_port     = 80
    protocol    = "tcp"
    cidr_blocks = ["0.0.0.0/0"]
  }

  egress {
    from_port   = 0
    to_port     = 0
    protocol    = "-1"
    cidr_blocks = ["0.0.0.0/0"]
  }
}

resource "aws_instance" "web" {
  ami                         = data.aws_ami.web.id
  instance_type               = "t3.micro"
  vpc_security_group_ids      = [aws_security_group.web.id]
  associate_public_ip_address = true

  metadata_options {
    http_tokens = "required"
  }

  tags = {
    Name = "web-nginx"
    AMI  = data.aws_ami.web.name
  }
}

output "public_ip" {
  value = aws_instance.web.public_ip
}

README.md

# Immutable web server

    cd packer
    packer init .
    packer validate .
    packer build .

    cd ../terraform
    terraform init
    terraform apply
    curl "http://$(terraform output -raw public_ip)"

The default VPC's subnets are public, so the instance gets a public address. No SSH port is open: to change the server, change the Packer template, build again and apply.

50 more in the member app, with practice sets and your own marks. Join free

More topics

Other interview topics.