Description
Modern web applications track user behavior using analytics SDKs that send events like "page viewed" or "button clicked" to a backend server.
In this challenge, you’ll build a simplified Analytics SDK that simulates how such an event queue works — with sequential sending, delays, and retry logic.
Your SDK will:
- Accept multiple events to be logged.
- Queue them internally.
- Send each event one after another (no parallel processing).
- Wait 1 second before sending each event.
- Simulate a failure every 5th attempt (e.g., 5th, 10th, 15th…).
- Retry failed sends immediately until they succeed.
- Only after an event succeeds should the next one begin.
Implementation Details
Implement a class AnalyticSDK with the following methods:
logEvent(event: string): void
- Adds the given event to an internal queue.
send(): Promise<void>
- Processes all queued events sequentially.
- For each event:
- Wait 1 second before sending.
- Attempt to send the event.
- If it fails (based on the failure rule), log
"failed <event>"and retry until it succeeds. - If it succeeds, log
"<event>".
- Continue until all events are successfully sent.
Example
Input
const sdk = new AnalyticSDK();
sdk.logEvent("event 1");
sdk.logEvent("event 2");
sdk.logEvent("event 3");
sdk.logEvent("event 4");
sdk.logEvent("event 5");
sdk.logEvent("event 6");
sdk.send();
Expected Output
event 1
event 2
event 3
event 4
failed event 5
event 5
event 6
Explanation
- Each event is sent one by one with a 1-second delay between them.
- The 5th attempt (
event 5) fails once, prints"failed event 5", retries, and then succeeds. - After that, processing continues normally with
event 6.
Constraints
0 <= number of events <= 100- Failure pattern: every 5th send attempt fails once.
- Use
Promisesorasync/await(no external libraries). - Events must be processed sequentially, not in parallel.
Hints
HInt 1
Use a counter (e.g., this.processed) to track total send attempts and simulate failure every 5th try.
HInt 2
Use setTimeout wrapped in a Promise to simulate the 1-second delay.
Hint 3
Make sure retry happens without delay — only the first send attempt for each event is delayed.