150Build an interactive coding playground with sandboxing, test execution, worker-based timeouts, and persisted solutions
hardAbout 90 minutesCodingsandboxingweb-workerstestingpersistence
Problem
Build the engine behind a coding-practice editor (like the one on this page): run a learner's code against a test suite safely, quickly and offline.
createPlayground({ storage, timeout = 2000 }) returns:
run(code, tests): executecodeand thentestsin a fresh, isolated Web Worker, and resolve with{ results, logs }:- inside the worker, provide
test(name, fn)(sync or async functions) andexpect(value)withtoBe,toEqual(deep equality) andtoThrow(message?); - tests run in order; each result is
{ name, passed, error? }(the assertion message for failures); console.loglines are captured inlogs;- if the code itself throws (or has a syntax error), resolve with
{ results: [], logs, error }; - remove
fetch,XMLHttpRequest,WebSocketandimportScriptsbefore running; - if the whole run exceeds
timeoutms (an infinite loop in code or a test), terminate the worker and reject with an error named"TimeoutError".
- inside the worker, provide
save(questionId, code)andload(questionId, fallback): persist each question's latest code as{ code, savedAt }JSON under"playground:{questionId}"instorage(getItem/setItem).loadreturnsfallbackfor missing or corrupt data.
Try it yourself
Write your solution, run it, then submit to check it against the tests.
App.jsxReact + DOM
Step-by-step
Build the worker source as a string and start it from a Blob URL: a separate thread with no DOM, no cookies, no access to the page, and a global scope you can strip further (network APIs) before any user code runs.
Unlock the full solution
What's inside
- Step-by-step
- Approach
- Solution
- Complexity
- Edge cases
- Follow-up questions
- Tests
Includes 2 code listings, 5 edge cases, 5 follow-up questions.
One-time payment by UPI, card or net banking. Launch price.