Implement popup and redirect in JavaScript
Company: Bitkernel
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: hard
Interview Round: Online Assessment
Overview: This question evaluates proficiency with client-side JavaScript, including browser APIs, DOM-based UI creation for popups, event handling, navigation mechanisms, and simple input validation for redirects.
Constraints
- 0 <= len(calls) <= 200000
- 0 <= requestedAt <= 10^9
- 0 <= closeAfter <= 10^9
- message is a string
- targetUrl may be any value; redirect only if targetUrl is a string with length greater than 0
- Calls should be processed by increasing requestedAt; calls with the same requestedAt keep their original input order
Examples
Input: ([(0, 'Saved', '/home', 2)],)
Expected Output: [[0, 'popup', 'Saved'], [2, 'redirect', '/home']]
Explanation: The popup appears at time 0 and closes at time 2. Since '/home' is a non-empty string, a redirect is recorded at time 2.
Input: ([(0, 'First', '/one', 3), (1, 'Second', '/two', 2)],)
Expected Output: [[0, 'popup', 'First'], [3, 'redirect', '/one'], [3, 'popup', 'Second'], [5, 'redirect', '/two']]
Explanation: The second call is requested at time 1, but the first popup blocks until time 3. The second popup therefore starts at time 3 and redirects at time 5.
Hints
- Sort the scheduled calls by requestedAt, but preserve original order for ties.
- Maintain one variable representing the earliest time when the blocking popup system is free.