PracHub
QuestionsCoachesLearningGuidesInterview Prep

Quick 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.

  • hard
  • Bitkernel
  • Coding & Algorithms
  • Software Engineer

Implement popup and redirect in JavaScript

Company: Bitkernel

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Take-home Project

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: 1. Immediately shows a popup dialog to the user displaying the given `message`. 2. 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.location` or a similar mechanism). - If `targetUrl` is 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`.

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.

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

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

  1. Sort the scheduled calls by requestedAt, but preserve original order for ties.
  2. Maintain one variable representing the earliest time when the blocking popup system is free.
Last updated: Jun 30, 2026

Loading coding console...

PracHub

Master your tech interviews with 8,000+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • AI Coding Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.