← Create New Paste

Designing an Online Code Judge (LeetCode-Style) in a System Design Interview

6 views
"Design an online judge where users submit code and get results" is an increasingly common system design prompt, partly because interviewers know candidates have used such platforms. The interesting parts are secure code execution, queueing under contest load, and fair result reporting. Requirements Functional: browse problems, submit code in several languages, run it against hidden test cases, return verdicts (Accepted, Wrong Answer, Time Limit Exceeded, Memory Limit Exceeded, Runtime Error, Compile Error), show submission history, and run timed contests with a live leaderboard. Non-functional: untrusted code must never escape its sandbox, results should return within a few seconds normally, and the system must absorb contest spikes where tens of thousands of users submit within minutes. High-level components - Problem service: problem statements, metadata, and pointers to test data stored in object storage. - Submission service: accepts code, stores it with status PENDING, and enqueues a job. - Job queue: durable queue (Kafka, SQS, or RabbitMQ) partitioned by language or priority. - Judge workers: pull jobs, compile and run code inside sandboxes, compare outputs, and write verdicts. - Result service: updates submission status and notifies the client through WebSocket or polling. - Contest and leaderboard service: computes rankings, usually with a sorted set in Redis. Sandboxing: the critical part Explain layered isolation. Each run happens in a fresh container or lightweight microVM (gVisor or Firecracker), with no network access, a read-only filesystem except a small temp directory, strict CPU and memory limits through cgroups, seccomp filters blocking dangerous system calls, a process count limit to stop fork bombs, and a wall-clock timeout. Run workers on dedicated hosts separate from the rest of the infrastructure. Interviewers often probe here, so be specific. Measuring time fairly CPU time is more stable than wall time, but both should be enforced. Pin each run to dedicated cores to reduce noisy-neighbor variance, and calibrate per-language time multipliers since Python is slower than C++. Test data handling Test cases can be large. Workers cache test data locally keyed by problem version, downloading from object storage only on cache misses. Comparing outputs may need special checkers for problems with multiple correct answers or floating-point tolerances. Handling contest spikes Pre-scale worker pools before scheduled contests, prioritize contest submissions over practice ones with separate queues, and apply per-user rate limits. Because the queue is durable, a backlog increases latency but does not lose submissions. Show queue position to users so the wait feels transparent. Leaderboard For each accepted submission during a contest, update the user's score and penalty time, and store it in a Redis sorted set with a composite score. Freeze the public leaderboard in the final minutes if the contest rules require it. Reliability and idempotency If a worker crashes mid-run, the job's visibility timeout expires and another worker picks it up. Verdict writes are idempotent per submission id. Scale estimate At 50,000 submissions in ten minutes and roughly 2 seconds of execution each, you need around 170 concurrent sandboxes on average, more for peaks, which is very achievable. Practicing this design out loud, especially the sandboxing details, builds confidence. For support in the moment, Linkjob AI (https://linkjob.ai) offers real-time help during job interviews and technical interviews.
Created Nov 15, 2026 23:29 Expires Nov 15, 2027 23:29