Coding questions · Bitkernel
Implement popup and redirect in JavaScript
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.
You are working in a browser environment and must use vanilla JavaScript only (no external libraries).
Task
Implement a function showPopupAndRedirect(message, targetUrl) that does the following:
- Immediately shows a popup dialog to the user displaying the given
message. - After the user closes the popup, the current page should redirect to
targetUrl.
Requirements
- The popup must be created using only built-in browser APIs (for example,
alert,confirm, or a custom DOM-based modal that you implement yourself). - The redirect must be done using standard browser navigation (e.g., assigning to
window.locationor a similar mechanism). - If
targetUrlis not a non-empty string, the function should not attempt to redirect. - Assume this code runs in a modern browser with JavaScript enabled.
You do not need to handle errors beyond the simple validation of targetUrl described above.
Describe the implementation and then provide the JavaScript code for showPopupAndRedirect.
Popup and Redirect Queue Simulator
In a browser, a function like showPopupAndRedirect(message, targetUrl) would immediately show a blocking popup, wait for the user to close it, and then redirect if targetUrl is a non-empty string. For this coding problem, you will simulate the observable order of actions for many scheduled calls to that function. Each call has a requested time, a popup message, a target URL, and the amount of time until the user closes the popup. Only one popup can be active at a time, so if another call is requested while a popup is still open, it must wait until the current popup closes. For this simulation, record redirect actions but continue processing later scheduled calls.
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
Example 1
Input: ([(0, 'Saved', '/home', 2)],)
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.
Example 2
Input: ([(0, 'First', '/one', 3), (1, 'Second', '/two', 2)],)
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.