Dashboard/System design/42. Design a LeetCode System

SYSTEM DESIGN · LESSON 42 OF 52 · 13 MIN VIDEO · 2 TOPICS

Design a LeetCode System

Module Progress (0 / 2 Lessons Completed)0%
System design Track (0 / 52 Modules) · Overall 0/80 (0%)0%
⌁

Narrated HD Video Lesson (13 min) · Pro

Lesson Overview + Section #01 + RESHADED Checklist + Quiz #1 are unlocked free below (no login required). Unlock all 80 HD video lessons and deep-dive sections — or watch our two free sample video lessons first.

0% READSec 01/02 · Introduction to the LeetCode System
SYSTEM DESIGN VIDEO LESSON & GUIDE

Design a LeetCode System

Designing an online coding and competitive programming platform like LeetCode pairs a read-heavy problem catalog with a hostile, CPU-intensive Remote Code Execution (RCE) judge engine. While serving problem pages and storing 10M submissions/day (20 GB/day) are straightforward web workloads, compiling and running arbitrary untrusted code from strangers—defending against infinite loops, fork bombs, memory exhaustion, and data exfiltration—within 2-5 seconds during 333 RPS synchronized contest spikes is an acute isolation and compute-elasticity problem. L4-L6 interviewers evaluate your sandbox defense-in-depth (hardened containers vs. microVMs), CPU-core capacity math, crash-safe visibility-timeout queues, and in-memory contest leaderboard structures.

WHAT YOU WILL MASTER IN THIS LESSON
  • Isolate untrusted user code from control-plane services using single-use ephemeral sandboxes enforced via seccomp syscall filters, Linux cgroups, read-only filesystems, disabled networking, or microVMs.
  • Translate submission arrival rates into CPU core-second demand (RPS * 2 core-s), distinguishing normal practice load (~8 32-core servers) from synchronized contest bursts (~21 servers).
  • Design crash-resilient asynchronous judge pipelines combining durable queues, warm sandbox pools, local SSD test-case caching, and acknowledge-after-persist semantics.
  • Protect live contests from practice-queue starvation and cold-start delays using priority queues, scheduled pre-scaling, and O(log N) Redis sorted-set leaderboards.
SYSTEMATIC INTERVIEW CHECKLIST · RESHADED + 45-MIN CLOCK

8-Step RESHADED Execution Checklist

Follow this exact sequence from the Intro narration on every system design prompt so you never miss requirements, math, contracts, or the 13-minute deep dive.

0 / 8 Checked
INTERVIEWER SCORECARD · WHAT MAKES A STRONG HIRE?

5 Signals Bar Raisers Grade (L4 vs. L5 Senior vs. L6 Staff)

Senior (L5 / E5 / Senior SDE) Bar — Autonomous Pacing, Depth & Proactive Trade-Offs: Owns the 45-minute clock without prompting, derives every component quantitatively, surfaces failure modes (hot shards, stampedes, dual writes) BEFORE being asked, and defends consistency vs. latency trade-offs.
1. Scoping
✓ STRONG HIRE SIGNAL

Clarifies users, read/write skew, p99 latency, consistency bounds, and explicit non-goals in minutes 2–8.

✗ NO-HIRE RED FLAG

Jumps straight into drawing load balancers and microservices at minute 2 without asking a single question.

2. Right-Sized Design
✓ STRONG HIRE SIGNAL

Starts with the simplest working architecture justified by the math (e.g., 1 NVMe SQL primary + replicas + Redis for 40 writes/s & 3 TB).

✗ NO-HIRE RED FLAG

Recites a memorized Kafka + multi-DC Cassandra diagram that contradicts their own traffic calculations.

3. Technical Depth
✓ STRONG HIRE SIGNAL

Drives deep into 1–2 hard bottlenecks (partition keys, hot-key mitigation, transactional outbox, KV-cache batching) at minute 27.

✗ NO-HIRE RED FLAG

Skims across 12 boxes at identical buzzword depth and runs out of time before the deep dive.

4. Explicit Trade-Offs
✓ STRONG HIRE SIGNAL

Uses 3-part reasoning: 'A queue here because SMTP spikes to 3 s; checkout stays <60 ms, and we accept eventual email delay + retry dedup.'

✗ NO-HIRE RED FLAG

Name-drops tools ('We put Kafka and ZooKeeper here') without stating the alternative or operational cost.

5. Communication
✓ STRONG HIRE SIGNAL

Thinks aloud every 30–45s, checks alignment at phase transitions, and treats 'What if X fails?' as a cue for where points are awarded.

✗ NO-HIRE RED FLAG

Goes silent for 5 minutes while drawing or defends a flawed assumption defensively when nudged.

ARCHITECTURE & DATA-FLOW SLIDES · SLIDE 1 OF 1Section 01: Introduction to the LeetCode System
ARCHITECTURE / DATA FLOW DIAGRAM
Coderbrowserout of memoryWeb serverpages + codeDatabaseproblems, testskilled: time limitJudgesandboxed
SECTION 01 OF 2

1. Introduction to the LeetCode System

★ FREE PREVIEW
Key Takeaway: Storage volume (7.3 TB/year) is trivial, whereas untrusted CPU execution (~2 core-seconds/submission) arrives in sharp contest spikes (333 RPS = 667 busy cores), mandating strict control-plane/judge separation and elastic sandboxed workers.
ARCHITECTURE / DATA FLOW DIAGRAM
Coderbrowserout of memoryWeb serverpages + codeDatabaseproblems, testskilled: time limitJudgesandboxed

Why Untrusted Code Execution Requires Physical Isolation

In a naive monolithic design where a web server compiles and executes submitted code directly inside request handlers, a single malicious or buggy submission brings down the entire platform. An infinite while(true) loop pins a CPU core indefinitely, unbounded heap allocations trigger the Linux OOM killer against the web process, reading local filesystem paths exposes hidden test-case answers, and opening outbound network sockets leaks secrets or turns servers into DDoS bots. Consequently, the architecture splits cleanly into two isolated planes: stateless web/API servers that manage problem statements and user sessions, and dedicated Judge Workers that execute each submission inside a sealed, networkless, single-use sandbox capped by hard 2 s CPU and 256 MB memory limits.

  • CPU Starvation Defense: Enforce strict CPU-time quotas (e.g., 2.0 s via cgroups) so infinite loops terminate cleanly as Time Limit Exceeded (TLE) without affecting siblings.
  • Memory & Process Caps: Cap resident memory (256 MB) and process count (pids.max = 64) so runaway allocations yield Memory Limit Exceeded (MLE) and fork bombs hit a hard kernel wall.
  • Zero Network & Read-Only Filesystem: Unshare network namespaces completely (loopback down, zero external NICs) and mount the root filesystem read-only with a tiny ephemeral tmpfs scratchpad.
  • Single-Use Ephemerality: Destroy every sandbox immediately after one submission so residual files, environment variables, or processes never leak across users.

Functional and Non-Functional Requirements

Beyond browsing problems and running ad-hoc sample inputs, users iterate rapidly—submitting solutions repeatedly and expecting deterministic verdicts within seconds—while weekly timed contests concentrate thousands of concurrent competitors onto the exact same four problems simultaneously. Fairness is a subtle but vital non-functional requirement: identical O(N log N) code must receive consistent runtime measurements and the same pass/fail verdict regardless of which worker machine judged it.

CategoryRequirementTarget & System Implication
FunctionalProblem Catalog & SearchBrowse and filter thousands of problems by topic tag, difficulty, and status with paginated reads.
FunctionalInteractive Run (Custom Input)Compile and run code against sample or user-supplied inputs without recording a permanent submission.
FunctionalOfficial Submit & VerdictJudge code against ~50 hidden test cases and return Accepted, Wrong Answer, TLE, MLE, RE, or CE.
FunctionalLive Timed ContestsRank 100,000+ concurrent contestants in real time by problems solved and penalty time.
FunctionalSubmission HistoryPersist every submission's source code, language, verdict, CPU runtime, and peak memory per user.
Non-FunctionalHostile-Code SecurityZero privilege escalation, host filesystem access, cross-tenant leakage, or outbound network access.
Non-FunctionalLow Verdict Latency & Burst ScaleDeliver verdicts in 2-5 s even when submission rates jump 3x instantaneously at contest start.
Non-FunctionalDeterministic FairnessMeasure CPU time (rusage / cgroup) on homogeneous hardware SKUs rather than noisy wall-clock time.

Back-of-the-Envelope Capacity Estimation

Assuming 1,000,000 (1M) daily active users submitting an average of 10 times per day (Run + Submit), daily volume is 10,000,000 (10M) executions, or 10,000,000 / 86,400 s ≈ 116 submissions/s on average. Unlike 1 ms API reads, compiling source code and executing ~50 test cases consumes ~2 core-seconds of compute per submission, meaning 116 sub/s * 2 core-s ≈ 232 CPU cores (~8 32-core servers) are continuously saturated under normal load. During a weekly contest where 100,000 participants submit 3 times within the opening 15 minutes (900 s), contest load alone hits 300,000 / 900 s ≈ 333 submissions/s—requiring 333 * 2 ≈ 667 CPU cores (~21 32-core servers) immediately at minute zero.

DimensionFormulaEstimateCapacity Planning Takeaway
Daily Submissions1M DAU * 10 submissions10M / dayAppend-heavy per-user history log.
Average Submission Rate10M / 86,400 s~116 sub/sModest HTTP ingress; heavy downstream CPU work.
Baseline Judge Compute116 sub/s * 2 core-s / 32 cores~232 cores (~8 servers)Steady-state worker fleet size (32-core nodes).
Contest Peak Compute(100K * 3 / 900 s) * 2 core-s / 32333 sub/s -> ~667 cores (~21 servers)Sharp instantaneous spike requires pre-warming, not slow reactive scaling.
Submission Storage10M/day * 2 KB * 365 days20 GB/day (7.3 TB/year)Easily stored in user-sharded SQL or wide-column tables.
Interview Signal: Interviewer Signal: Always convert submission RPS into CPU core-seconds (RPS * avg_exec_time_s). Candidates who estimate 116 RPS = 1 server forget that untrusted compilation and 50-test execution is 1,000x heavier than a JSON CRUD lookup.
SECTION 02 OF 2

2. Design of the LeetCode System

🔒 PRO SECTION
Key Takeaway: Decouple stateless API servers from warm microVM/hardened-container judge workers via priority queues, cache blob-stored test cases on worker SSDs, acknowledge jobs only after persisting verdicts, and rank contests in an in-memory Redis sorted set.

Section 1 + Checkpoint Quiz #1 are unlocked free outside the paywall. Unlock all architecture slides, QPS & sharding trade-off tables, and all 80 narrated HD video lessons with Full Access — or inspect our sample payment system lesson first.

ACTIVE RECALL & TRADE-OFF PROBES

System Design Checkpoint Quizzes (1)

CHECKPOINT QUIZ #1

In "Introduction to the LeetCode System" (Functional and Non-Functional Requirements), how does a production-grade design handle "Functional" (Target & System Implication)?