System Design AI

Design an online judge like LeetCode: the junior, senior and staff answer

If you have practised coding interviews, you have used an online judge. You write a function in the browser, press submit, and a few seconds later learn whether it passed every hidden test or exactly where it failed. A few times a month there is a contest: everyone gets the same problems at the same second and races on a live leaderboard.

Two things make it harder than its size suggests. Every submission is a stranger's program you have promised to run, so the judge must treat each one as an attack. And the load follows a calendar: a contest's peak is many times a normal evening's.

Requirements to pin down

Functional:

  1. Browse problems and open one with a code stub in your language.
  2. Submit and get a verdict: accepted, or the first failing test with input, expected and actual output, or a time, memory, runtime or compile error.
  3. Run against sample tests or your own input, without it counting.
  4. Contests with a live leaderboard.

Non-functional, with the targets the worked design sets:

  • Isolation: no submission reaches the network, the host, another submission or the expected answers.
  • Verdicts in seconds: p50 under 2 seconds and p99 under 5, including the wait in the queue.
  • Contests of 100,000 people, with nothing lost at the peak and the leaderboard within 10 seconds of a verdict.
  • Fair: the same code gets the same verdict every time.

The numbers

  • Practice: 500,000 submissions a day is about 6 a second on average (500,000 / 86,400). Say 30 a second at the evening peak.
  • A contest: 100,000 people submitting about 10 times each is 1 million submissions in 90 minutes. That averages 185 a second (1,000,000 / 5,400 s), with bursts near 1,000 a second.
  • Sandboxes: at about one CPU-second per submission, 1,000 a second means about 1,000 sandboxes busy at once.

The number that shapes the design: the contest peak is about 30 times practice's, and you know the date days in advance.

The API

HTTP
POST /v1/problems/42/submissions      Idempotency-Key: 7d1c...
{ "language": "python", "code": "...", "contestId": 9 }
-> 202 { "submissionId": "s_81f3" }

GET /v1/submissions/s_81f3
-> 200 { "status": "done", "verdict": "WA", "failedTest": { ... } }

The user comes from the session token and the time from the server's clock. A client-supplied timestamp would let anyone backdate an accepted answer.

The junior answer: a queue, workers, a throwaway container

  • The API stores the submission as queued, puts its id on a queue, and returns 202. The browser polls for the verdict.
  • A judge worker takes the job and runs the code in a fresh container: no network, a read-only filesystem, CPU, memory and process limits, no capabilities, a non-root user.
  • The container gets the test inputs only, never the expected outputs. The worker compares. A program that could read the expected answers could simply print them.
  • A double click is harmless because the idempotency key is unique per user. A job delivered twice cannot change a verdict that is already written.
  • Contest scores are recomputed from the contestant's own submissions, not incremented. A job can run twice and verdicts can arrive out of order; a counter drifts, a recomputation does not.

Where it breaks, said with numbers: containers share the host's kernel, so one kernel bug reachable from a submission is an escape. And hosts added when the queue grows arrive minutes after the contest rush has started.

What a senior answer adds

  • A real comparison of where code runs. In the API: never. A full VM per submission: strong isolation, but slow to boot and heavy. A container: fast, but a shared kernel. Serverless functions: no fleet, but warm environments are reused between invocations and you get little control of the sandbox. A microVM per submission: its own guest kernel, and restored from a per-language snapshot it starts in milliseconds. The worked design picks Firecracker microVMs, used once and destroyed.
  • Capacity from the calendar. A queue per class of work (contest, practice, runs), contest drained first. The judge fleet is grown before the contest starts, sized from registrations, rather than chasing CPU alarms.
  • Polling over push, with a reason. Push would save under a second on an answer that takes about two, and polling survives deploys.
  • A leaderboard that ranks in log time. One sorted set per contest, with score = solved x 1,000,000 - penalty seconds. A page is a range read in reverse order; "my rank" is the count of strictly higher scores plus one.
  • One test set for every language. A language-neutral set per problem, with a generated harness per language, instead of writing the tests eight times.

What a staff answer adds

  • Isolation as layers. Each wall assumes the one inside it failed: a non-root process under a system-call allowlist, then the microVM's guest kernel, then a jailed VMM, then hosts with no credentials that are replaced regularly, then a separate account for judging.
  • Judges that cannot write the database. Verdicts go to a results queue, and a writer accepts one only with the lease token issued with that job, so a compromised host can misjudge only the jobs it holds.
  • Fair timing. A whole physical core per VM, one instance type for a whole contest, CPU time rather than wall time, and a re-run on another host for verdicts close to the limit.
  • Verdicts as versioned facts. Each verdict is stored with the test set version and runtime it ran on. When a test turns out to be wrong, a new version is published, affected submissions are rejudged on spare capacity, and the old verdicts are kept beside the new ones.
  • Operations. Copied solutions found by fingerprinting after the contest, configuration that cannot change during one, and a written policy for what happens when each part fails mid-contest.

Mistakes that sink this question

  • Running submitted code anywhere near the API.
  • Letting the sandbox see the expected outputs.
  • Holding the submit request open until the verdict exists.
  • Incrementing scores instead of recomputing them.
  • Trusting the client's clock for the penalty.
  • Autoscaling on CPU for a load spike you knew about a week ago.

Practise it

Read the free worked design, Design an Online Judge like LeetCode: start with the board for your level, then read the next one up. Try the problem on paper for 45 minutes before you open it.

With Pro ($10/month) you can draw your answer on the practice canvas and get an AI review against the rubric.

Questions people ask

Why not run each submission in a serverless function?

It scales from zero with no fleet, but serverless platforms reuse warm environments across invocations, so one submission could leave something behind for the next. You also get little control over the sandbox, such as a custom system-call filter or pinning to a core. For hostile code you want a fresh sandbox every time.

Should the verdict be pushed over a WebSocket instead of polled?

It can be, but polling is simpler and good enough here: with a median verdict under two seconds, one or two polls usually find it, and polling survives restarts. Push pays off when verdicts are sub-second, or when you also stream ranks and announcements.

How do you stop one contestant's code slowing another's?

Give each sandbox its own physical core and memory limit, measure CPU time rather than wall-clock time, run a whole contest on one machine type, and re-run any verdict that lands close to the time limit on a second host.