FrontendPro

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): execute code and then tests in a fresh, isolated Web Worker, and resolve with { results, logs }:
    • inside the worker, provide test(name, fn) (sync or async functions) and expect(value) with toBe, toEqual (deep equality) and toThrow(message?);
    • tests run in order; each result is { name, passed, error? } (the assertion message for failures);
    • console.log lines are captured in logs;
    • if the code itself throws (or has a syntax error), resolve with { results: [], logs, error };
    • remove fetch, XMLHttpRequest, WebSocket and importScripts before running;
    • if the whole run exceeds timeout ms (an infinite loop in code or a test), terminate the worker and reject with an error named "TimeoutError".
  • save(questionId, code) and load(questionId, fallback): persist each question's latest code as { code, savedAt } JSON under "playground:{questionId}" in storage (getItem/setItem). load returns fallback for 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.

All 150 answersOne payment, yours forever