LC-TEST-16 — Algorithm Test (V3)

🌍 Why This Document Exists

This document trains engineering judgment, not syntax.

Real interviews test:

  • how you combine conditions
  • how you eliminate wrong paths
  • how you protect systems with logic

If you master this → you think like a senior Python engineer.


🧠 Python Algorithm Thinking — 20 Advanced Logic Puzzles

Rules:

  • No shortcuts
  • Read carefully
  • Think in conditions

🟢 Puzzle 1 — Smart API Throttling

API request has:

requests = [
    {"user":"A","count":90,"premium":False},
    {"user":"B","count":120,"premium":True},
    {"user":"C","count":120,"premium":False},
]

Rules:

  • Block if count > 100
  • BUT allow if premium == True

Task:
Return users that are blocked.

✅ Solution
blocked = []

for r in requests:
    if r["count"] > 100 and not r["premium"]:
        blocked.append(r["user"])

print(blocked)

🟢 Puzzle 2 — Secure Login Validator

A login attempt is valid if:

  • username exists
  • password matches
  • account is active OR admin override is true
user = {
  "exists": True,
  "password_ok": True,
  "active": False,
  "admin_override": True
}

Task:
Return True / False.

✅ Solution
valid = (
    user["exists"]
    and user["password_ok"]
    and (user["active"] or user["admin_override"])
)

print(valid)

🟢 Puzzle 3 — Production Error Escalation

Error should be escalated if:

  • severity ≥ 8
  • AND (service is "payment" OR "auth")
  • AND NOT during maintenance
error = {"sev":9,"service":"payment","maintenance":False}
✅ Solution
if (
    error["sev"] >= 8
    and error["service"] in ("payment","auth")
    and not error["maintenance"]
):
    print("Escalate")

🟡 Puzzle 4 — Smart Cache Usage

Use cache if:

  • cache exists
  • AND (data fresh OR forced_cache=True)
cache = {"exists":True,"fresh":False}
forced_cache = True
✅ Solution
use_cache = (
    cache["exists"]
    and (cache["fresh"] or forced_cache)
)

print(use_cache)

🟡 Puzzle 5 — Fraud Detection Rule

Transaction is suspicious if:

  • amount > 10_000
  • OR (amount > 3_000 AND country not trusted)
  • AND NOT internal_transfer
tx = {"amount":5000,"trusted":False,"internal":False}
✅ Solution
if (
    (tx["amount"] > 10000
    or (tx["amount"] > 3000 and not tx["trusted"]))
    and not tx["internal"]
):
    print("Suspicious")

🟡 Puzzle 6 — Multi-Level Access Control

Access granted if:

  • role is "admin"
  • OR (role "dev" AND env "staging")
  • OR (role "viewer" AND read_only=True)
user = {"role":"dev","env":"staging","read_only":False}
✅ Solution
if (
    user["role"] == "admin"
    or (user["role"] == "dev" and user["env"] == "staging")
    or (user["role"] == "viewer" and user["read_only"])
):
    print("Access granted")

🟡 Puzzle 7 — Intelligent Retry Logic

Retry request if:

  • status >= 500
  • AND retries < 3
  • AND NOT timeout
req = {"status":502,"retries":2,"timeout":False}
✅ Solution
if (
    req["status"] >= 500
    and req["retries"] < 3
    and not req["timeout"]
):
    print("Retry")

🔵 Puzzle 8 — Data Pipeline Drop Rule

Drop record if:

  • missing required field
  • OR invalid format
  • OR (late arrival AND not backfill)
rec = {"missing":False,"invalid":False,"late":True,"backfill":False}
✅ Solution
if (
    rec["missing"]
    or rec["invalid"]
    or (rec["late"] and not rec["backfill"])
):
    print("Drop")

🔵 Puzzle 9 — Alert Noise Reduction

Send alert if:

  • severity ≥ 7
  • AND (new_error OR error_rate > threshold)
  • AND NOT already_alerted
✅ Solution
if (
    sev >= 7
    and (new_error or rate > threshold)
    and not alerted
):
    print("Alert")

🔵 Puzzle 10 — Smart Autoscaling Decision

Scale up if:

  • cpu > 70 AND mem > 70
  • OR queue > 1000
  • AND NOT maintenance
✅ Solution
if (
    ((cpu > 70 and mem > 70) or queue > 1000)
    and not maintenance
):
    print("Scale up")

🔴 Puzzle 11 — Complex Log Filtering

Keep log if:

  • level is ERROR or WARN
  • AND service not deprecated
  • AND (env is prod OR flagged)
✅ Solution
if (
    log["level"] in ("ERROR","WARN")
    and not log["deprecated"]
    and (log["env"] == "prod" or log["flagged"])
):
    kept.append(log)

🔴 Puzzle 12 — Deployment Gate

Allow deploy if:

  • tests passed
  • AND security approved
  • AND (low_risk OR manager_approved)
✅ Solution
if (
    tests_ok
    and security_ok
    and (low_risk or manager_ok)
):
    print("Deploy")

🔴 Puzzle 13 — Feature Flag Evaluation

Enable feature if:

  • flag_on
  • AND user_beta
  • AND NOT region_blocked
✅ Solution
if flag and beta and not blocked:
    enable()

🔴 Puzzle 14 — Dead Letter Queue Rule

Send to DLQ if:

  • retries exhausted
  • OR malformed payload
  • AND NOT manual_override
✅ Solution
if (
    (retries == 0 or malformed)
    and not manual_override
):
    send_dlq()

🔴 Puzzle 15 — AI Model Safety Gate

Allow response if:

  • confidence > 0.9
  • AND NOT hallucinated
  • AND (verified_source OR human_reviewed)
✅ Solution
if (
    conf > 0.9
    and not hallucinated
    and (verified or reviewed)
):
    respond()

🟣 Puzzle 16 — Distributed Lock Acquisition

Acquire lock if:

  • lock free
  • OR expired
  • AND requester authorized
✅ Solution
if (
    (free or expired)
    and authorized
):
    acquire()

🟣 Puzzle 17 — Multi-Region Failover

Failover if:

  • primary down
  • AND secondary healthy
  • AND NOT maintenance_window
✅ Solution
if primary_down and secondary_ok and not maintenance:
    failover()

🟣 Puzzle 18 — Abuse Detection

Block user if:

  • reports > 5
  • AND (spam OR harassment)
  • AND NOT verified_user
✅ Solution
if reports > 5 and (spam or harassment) and not verified:
    block()

🟣 Puzzle 19 — Financial Transaction Approval

Approve if:

  • balance sufficient
  • AND not frozen
  • AND (trusted_merchant OR small_amount)
✅ Solution
if balance >= amount and not frozen and (trusted or amount < 50):
    approve()

🟣 Puzzle 20 — Senior-Level Thinking

Explain (no code):
Why grouping conditions incorrectly can cause:

  • security bugs
  • outages
  • financial loss
✅ Solution

Because logical precedence mistakes (and / or) can:

  • bypass security
  • trigger actions unintentionally
  • scale systems incorrectly

Senior engineers design logic defensively.


🏁 Final Truth

If you master logic composition,
you master engineering power.

Teach this. Spread this. Make engineers better.


Previous
Next