Full answer
Requirements
Functional:
- Users submit code in several languages for a problem; they get a verdict (Accepted, Wrong Answer, Time Limit Exceeded, Runtime Error, Compilation Error) and run time.
- "Run" against sample tests and "Submit" against hidden tests.
- Contests with a live leaderboard.
Non-functional:
- User code can never read other users' data, the hidden tests, or reach the network.
- Verdicts in a few seconds normally; queues may grow during contests but must not lose submissions.
- Fair and repeatable timing.
Capacity estimates
Assumptions: 2M submissions per day normally; a contest with 50,000 users each submitting every 5 minutes; 5 CPU-seconds per submission across all test cases.
- Normal: 2M / 86,400 = about 23 submissions per second.
- Contest: 50,000 / 300 = about 167 per second.
- CPU: 167 x 5 = about 830 cores busy; with 2x headroom about 1,700 cores, autoscaled for the contest window only.
API
POST /v1/submissions {"problem_id": "two-sum", "language": "python3", "source": "..."}
-> 202 {"submission_id": "s_731", "status": "QUEUED"}
GET /v1/submissions/{id} -> {"status": "ACCEPTED", "time_ms": 84, "memory_kb": 10240, "tests_passed": 42}
WS /v1/submissions/{id}/events (live status: COMPILING, RUNNING 17/42, ...)
Data model
problems: problem_id, statement, limits (time per test, memory), checker type.test_cases: problem_id, case_id, input and expected output stored in object storage, readable only by judge workers.submissions: submission_id, user_id, problem_id, language, source location, status, verdict, time_ms, memory_kb, created_at.
High-level design
flowchart LR
U[User] --> API[Submission API]
API --> DB[(Submissions DB)]
API --> Q[(Submission queue)]
Q --> W[Judge workers]
W --> SB[Sandbox per run]
W --> TC[(Test cases store)]
W --> DB
W -- "status events" --> PUB[Pub-sub]
PUB --> API
API -- "WebSocket" --> USandboxing
Each run happens in a fresh sandbox:
- A container with seccomp filters and no capabilities, or a microVM (Firecracker, gVisor) for stronger isolation.
- No network interface; read-only root file system; a small writable temp directory.
- Limits through cgroups: CPU time per test, wall-clock time, memory, number of processes (to stop fork bombs), output size.
- Runs as an unprivileged user; the sandbox is destroyed afterwards.
The worker copies in only the source and the current test input, never the whole test set, and compares output outside the sandbox (exact match, token match, or a special checker for problems with many correct answers).
Bottlenecks and trade-offs
- Contest spikes. Pre-warm workers before the start, use the queue to absorb bursts, and give contest submissions priority over practice runs.
- Timing fairness. Noisy neighbours change run times; pin each sandbox to dedicated cores and measure CPU time, not wall time.
- Containers versus microVMs. Containers start in milliseconds but share the kernel; microVMs give stronger isolation at a small start-up cost. Pools of pre-started sandboxes hide that cost.
- Language images are cached on every worker to avoid pulling them at contest start.
- Duplicate processing. If a worker dies, the queue redelivers; the verdict write is idempotent by submission_id.
By level
Fresher: Explain that submissions wait in a queue and workers run them in an isolated box with time and memory limits. A fresher should list at least three things the sandbox must block.
Mid: Do the contest math, describe sandbox limits and isolation choices, and stream status updates. Handle worker crashes with idempotent verdicts.
Senior: Compare containers, gVisor and microVMs, design for timing fairness, prioritisation, pre-warming and special checkers. Expect a threat model that covers escape attempts and data leaks.
Key points
- Queue between API and judge workers absorbs spikes
- Fresh sandbox per run with no network and strict cgroup limits
- Test cases never enter the sandbox in bulk; compare outside
- Measure CPU time for fair verdicts
- Pre-warm and prioritise for contests
Common mistakes
- Running user code directly on the API server
- Allowing network access inside the sandbox
- No limit on processes or output size
- Using wall-clock time on shared machines for verdicts
Follow-ups
- How would you support interactive problems that talk to a judge program?
- How do you detect plagiarism in a contest?
- How would you add a new language safely?
Tips
- Start with the threat model: untrusted code
- List the concrete sandbox limits
- Do the contest CPU math
Evaluation rubric
- Clear async pipeline with a queue
- Strong sandbox design with specific limits
- Capacity math for contests
- Fairness and failure handling