Problem, as an interviewer would state it: "Implement an auto-reconnecting WebSocket wrapper: when the connection closes unexpectedly, retry with exponential backoff, resetting the backoff once a connection genuinely succeeds."
Examples:
const socket = createReconnectingSocket(() => new WebSocket(url));
// on a real disconnect, retries after 1s, 2s, 4s, 8s... up to a max delay
Clarifying questions expected:
- Should the backoff RESET to the base delay after a real successful reconnection, or keep growing indefinitely across the whole session?
- Is there a maximum delay cap, or does the backoff grow unbounded?
- Should a real, deliberate
.close()call by the application itself trigger a reconnect attempt, or only an unexpected disconnect?
Code / implementation expected: Yes — real, direct proof against a mock socket that genuinely fails a fixed number of times before succeeding, confirming the real, measured backoff delays correctly double and the connection eventually recovers.