Implement popup and redirect in JavaScript
Company: Bitkernel
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: hard
Interview Round: Take-home Project
Quick Answer: 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.
Input: ([(5, 'Late', '/late', 1), (0, 'No target', None, 2), (1, 'Empty URL', '', 0)],)
Expected Output: [[0, 'popup', 'No target'], [2, 'popup', 'Empty URL'], [5, 'popup', 'Late'], [6, 'redirect', '/late']]
Explanation: Calls are processed by requested time, not input order. None and the empty string are invalid redirect targets, so only the '/late' call creates a redirect.
Input: ([],)
Expected Output: []
Explanation: There are no scheduled calls, so no popup or redirect actions occur.
Input: ([(0, 'A', '/a', 0), (0, 'B', '/b', 1), (0, 'C', '', 1)],)
Expected Output: [[0, 'popup', 'A'], [0, 'redirect', '/a'], [0, 'popup', 'B'], [1, 'redirect', '/b'], [1, 'popup', 'C']]
Explanation: All calls have the same requested time, so original order is preserved. The first popup closes immediately, so its redirect and the next popup both occur at time 0, with the redirect recorded first.
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.