How Leetcode’s Sandbox Keeps Your Code Safe and Fast
Leetcode runs each submission in a isolated Linux container with cgroup v2 limits and seccomp‑bpf syscall filtering, ensuring safe and repeatable execution.
01 Jun 2026, 21:11 UTC

The Problem: Untrusted Code on a Public Platform
When you click “Run Code” on Leetcode, your solution is executed in a shared environment alongside thousands of other submissions. If the platform could not guarantee isolation, a malicious or buggy program could steal CPU cycles, exhaust memory, or even attempt to reach the network. The core challenge is therefore to provide each user’s code with a fast, repeatable execution sandbox that protects both the service and other users.
How Leetcode isolates submissions
Leetcode builds a lightweight Linux container for every submission. The container is started with strict resource limits enforced by cgroups v2 and a seccomp‑bpf filter that allows only a small set of system calls. After the program finishes—or is terminated for exceeding a limit—the container is destroyed, leaving no persistent state.
Resource limits with cgroups v2
Cgroups v2 cap the amount of CPU time and memory a process may use. If a submission tries to allocate more memory than the allowed quota, the kernel sends a SIGKILL and Leetcode returns a “Memory Limit Exceeded” verdict. Similarly, a busy loop that consumes more CPU time than the slice triggers a “Time Limit Exceeded” result. Because the limits are applied at the kernel level, they are difficult to bypass from within the sandbox.
Syscall filtering with seccomp-bpf
The seccomp profile permits calls such as read, write, exit, futex, and a few others needed for typical language runtimes. Calls like clone, openat with certain flags, or socket are blocked. When a disallowed syscall is invoked, the kernel terminates the process with SIGSYS, which Leetcode reports as a generic “Runtime Error”. This drastically reduces the attack surface: even if a program tries to spawn new threads or open files in unexpected ways, the filter stops it before any harmful effect can occur.
Worked example: probing the sandbox
To see the isolation in action, you can submit a short program that prints its process ID and checks for network interfaces. The following C++ snippet does that using only allowed syscalls:
#include <iostream>
#include <unistd>
#include <sys/sysinfo>
int main() {
std::cout << "PID: " << getpid() << std::endl;
// Attempt to list network interfaces via /proc/net/dev (read only)
FILE* f = fopen("/proc/net/dev", "r");
if (f) {
char line[256];
while (fgets(line, sizeof(line), f)) {
std::cout << line;
}
fclose(f);
} else {
std::cout << "Cannot open /proc/net/dev" << std::endl;
}
return 0;
}
If you run this snippet on Leetcode, the output will show a PID that belongs to the container’s isolated process tree (typically a high number in a restricted range) and the contents of /proc/net/dev will list only the loopback interface, confirming that no external network sockets are available. The program uses only getpid (via getpid syscall) and fopen/read on a proc file, both of which are permitted by the seccomp filter.
Trade‑offs and limitations
- Disk I/O: The sandbox limits CPU and memory but does not cap the amount of data a process can write to its own filesystem. A tight loop that repeatedly writes large files could fill the container’s disk and degrade performance for other users sharing the same host, leading to a denial‑of‑service style effect.
- Language‑specific warm‑up: Languages such as Java or .NET rely on just‑in‑time compilation. The first few executions may spend time warming up the JIT, which can cause a submission that is actually within the algorithmic limits to be judged as “Time Limit Exceeded” on Leetcode. The platform mitigates this by giving each language a slightly higher time budget, but variability remains.
Actionable takeaway
When preparing solutions, focus on algorithmic efficiency rather than trying to micro‑optimize system calls. Avoid patterns that generate excessive file I/O (e.g., writing large logs or temporary files) because they are not protected by the sandbox’s cgroup limits. If you are using a JVM‑based language, consider adding a small warm‑up run or using the latest language version that Leetcode provides, to reduce the chance of a false time‑limit verdict. Understanding the sandbox’s mechanisms helps you write code that runs reliably within the platform’s limits.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.