Opscraft
Python & Bash automation · Lesson 3 of 4 · 5 min read

Classic coding-round problems with solutions

1. Top 10 client IPs from an access log

Bash:

awk '{print $1}' access.log | sort | uniq -c | sort -rn | head -10

Python (streaming, memory-safe for large files):

from collections import Counter
counts = Counter()
with open("access.log", encoding="utf-8", errors="replace") as f:
    for line in f:
        ip = line.split(" ", 1)[0]
        counts[ip] += 1
for ip, n in counts.most_common(10):
    print(n, ip)
Enlarge diagramaccess.log20 GB on diskfor line in fone line in memory at atimeCounter[ip] += 1one entry per distinctIPmost_common(10)the answer
Figure 1. Reading a huge log safely: one line at a time into a counter, so memory depends on the number of distinct IPs, not the size of the file.
Swipe sideways to see all of it, or tap to open it full screen.

2. Count HTTP status codes and the 5xx ratio per minute

Assuming a combined log format (... [27/Oct/2026:10:15:32 +0530] "GET /x HTTP/1.1" 503 ...):

import re
from collections import defaultdict

pat = re.compile(r'\[(\d{2}/\w{3}/\d{4}:\d{2}:\d{2}):\d{2} [^\]]+\] "[^"]*" (\d{3})')
total, errors = defaultdict(int), defaultdict(int)
with open("access.log") as f:
    for line in f:
        m = pat.search(line)
        if not m:
            continue
        minute, status = m.group(1), m.group(2)
        total[minute] += 1
        if status.startswith("5"):
            errors[minute] += 1
for minute in sorted(total):
    print(f"{minute} total={total[minute]} 5xx={errors[minute]} ratio={errors[minute]/total[minute]:.2%}")

Bash one-liner for status code counts: awk '{print $9}' access.log | sort | uniq -c | sort -rn.

3. Disk usage alert across hosts

#!/usr/bin/env bash
set -euo pipefail
threshold=${1:-80}
df -P -x tmpfs -x devtmpfs | awk -v t="$threshold" 'NR>1 {gsub("%","",$5); if ($5+0 >= t) printf "%s %s%% %s\n", $6, $5, $1}'

4. Find the largest files and directories

du -xh --max-depth=1 /var 2>/dev/null | sort -rh | head -15
find / -xdev -type f -size +500M -printf '%s %p\n' 2>/dev/null | sort -rn | head -20 | numfmt --to=iec --field=1

5. Validate that all Kubernetes Deployments have resource limits

from kubernetes import client, config
config.load_kube_config()
apps = client.AppsV1Api()
missing = []
for d in apps.list_deployment_for_all_namespaces().items:
    for c in d.spec.template.spec.containers:
        lim = (c.resources.limits or {}) if c.resources else {}
        if "memory" not in lim:
            missing.append(f"{d.metadata.namespace}/{d.metadata.name}:{c.name}")
print("\n".join(missing) or "all containers have memory limits")

6. Rotate/expire: delete S3 objects older than N days under a prefix (prefer lifecycle rules!)

from datetime import datetime, timedelta, timezone
import boto3
s3 = boto3.client("s3")
cutoff = datetime.now(timezone.utc) - timedelta(days=30)
batch = []
for page in s3.get_paginator("list_objects_v2").paginate(Bucket="acme-tmp", Prefix="exports/"):
    for obj in page.get("Contents", []):
        if obj["LastModified"] < cutoff:
            batch.append({"Key": obj["Key"]})
        if len(batch) == 1000:                       # delete_objects limit
            s3.delete_objects(Bucket="acme-tmp", Delete={"Objects": batch, "Quiet": True})
            batch = []
if batch:
    s3.delete_objects(Bucket="acme-tmp", Delete={"Objects": batch, "Quiet": True})

Mention in the interview: an S3 lifecycle rule does this with zero code — the script is for one-off cleanups or logic lifecycle can't express.

7. Health-check a list of URLs concurrently and exit non-zero on failure

import sys, requests
from concurrent.futures import ThreadPoolExecutor

def probe(url):
    try:
        r = requests.get(url, timeout=5)
        return url, r.status_code, r.elapsed.total_seconds()
    except requests.RequestException as e:
        return url, None, str(e)

urls = [l.strip() for l in open("urls.txt") if l.strip()]
with ThreadPoolExecutor(20) as ex:
    results = list(ex.map(probe, urls))
bad = [r for r in results if r[1] is None or r[1] >= 400]
for r in results:
    print(*r)
sys.exit(1 if bad else 0)

8. Parse YAML and diff two environment configs

import yaml, sys
a = yaml.safe_load(open(sys.argv[1]))   # safe_load — never yaml.load on untrusted input
b = yaml.safe_load(open(sys.argv[2]))

def diff(x, y, path=""):
    if isinstance(x, dict) and isinstance(y, dict):
        for k in sorted(set(x) | set(y)):
            diff(x.get(k), y.get(k), f"{path}.{k}" if path else k)
    elif x != y:
        print(f"{path}: {x!r} -> {y!r}")
diff(a, b)

Approach in a live coding round

  1. Clarify input format, size (does it fit in memory?), edge cases (malformed lines, empty input).
  2. Say the simple approach first (often a one-liner), then write clean code: functions, names, error handling.
  3. Stream large files line by line; use Counter, defaultdict, re precompiled.
  4. Test on a tiny sample; mention complexity (O(n) time, O(unique keys) memory).
  5. Talk about production hardening: logging, exit codes, retries, dry-run, tests.
Enlarge diagramClarifyinput format,size, edge casesSimple firstsay theone-liner, thenwrite clean codeStream biginputsline by line;Counter,defaultdictTest on asamplestate time andmemory costHardenlogging, exitcodes, retries,dry run12345
Figure 2. A calm approach to a live coding round.
Swipe sideways to see all of it, or tap to open it full screen.