KatInterview

Build a Secure Remote Code Grading Service for Contests

System DesignModel answer
MediumModel answerSystem Designsystem-designsandboxingmessage-queue

Last updated

Design a distributed service that safely runs untrusted submissions against hidden tests, returns verdicts reliably, and stays responsive during contest traffic bursts.

Scenario

You are asked to design a platform where programmers write a solution to a problem, submit it in one of many languages, and get an automatic verdict after the code is run against a private set of tests. Think of the grading backend of a competitive programming site.

What the system must do

  • Accept a submission (source code, language, target problem, optional contest).
  • Compile it if the language requires it, run it against the hidden tests, and report one of: Accepted, Wrong Answer, Time Limit Exceeded, Runtime Error, or Compilation Error.
  • Let users look up their past submissions.

Quality requirements

  • Security: the submitted code is hostile by assumption. It must not be able to read files, reach the network, or harm the host or other users.
  • Speed: verdicts should arrive quickly, and the platform must survive the surges that happen at the start and end of a contest.
  • Scale: thousands of submissions in flight at once.
  • Durability and consistency: an accepted submission is never lost, and its verdict is stable and stored reliably.

What to cover in your answer

  1. A text description of the high-level architecture.
  2. The API for submitting code and retrieving results.
  3. Your sandboxing approach for running untrusted programs.
  4. The data model and storage choices.
  5. How you keep the system healthy under peak load.

State your assumptions about scale (users, submissions per second, work per submission) and justify your choices with rough capacity math and trade-offs.

Hints

Hint 1

Start with numbers: estimate peak submissions per second times average CPU-seconds per submission to get the size of the worker fleet. This tells you the design must be asynchronous and elastic.

Hint 2

Separate accepting a submission from judging it. Persist the submission first, then enqueue it, and let workers pull jobs so that load shows up as queue depth rather than errors.

Hint 3

Assume any single isolation layer can be broken. Think in layers: a stronger boundary than a plain container, no network, restricted syscalls, cgroup limits, and output caps. Then map each verdict to a concrete signal the sandbox can report.

Hint 4

Queues deliver at least once. Think about what happens when a worker crashes mid-run or two workers finish the same job, and how a conditional state update plus retries and a dead-letter queue handle it.