Design a window manager that tracks focus order with O(1) open, close and top queries
Company: Zynga
Role: Software Engineer
Category: Software Engineering Fundamentals
Difficulty: medium
Interview Round: Other
Design a `WindowManager` that tracks open windows on a desktop and the order in which they were focused. The most recently focused window sits on top of the stack.
Opening a window that is already open should bring it to the top (re-focus it). Closing a window removes it entirely. Querying the top window returns the `windowId` of the most recently focused window, or `-1` if no windows are open.
All three operations must run in `O(1)` time.
Implement the following methods:
- `openWindow(windowId: int)`: Opens the window with the given id, or re-focuses it if already open, bringing it to the top.
- `closeWindow(windowId: int)`: Closes the window with the given id. No-op if the window is not open.
- `getTopWindow() -> int`: Returns the `windowId` of the most recently focused window, or `-1` if no windows are open.
Example 1:
```text
Input:
["WindowManager", "openWindow", "openWindow", "getTopWindow", "openWindow", "getTopWindow", "closeWindow", "getTopWindow", "closeWindow", "getTopWindow"]
[[], [1], [2], [], [3], [], [3], [], [2], []]
Output: [null, null, null, 2, null, 3, null, 2, null, 1]
Explanation:
wm = WindowManager() # no windows open
wm.openWindow(1) # stack: [1]
wm.openWindow(2) # stack: [2, 1]
wm.getTopWindow() # -> 2 (most recently focused)
wm.openWindow(3) # stack: [3, 2, 1]
wm.getTopWindow() # -> 3
wm.closeWindow(3) # stack: [2, 1]
wm.getTopWindow() # -> 2
wm.closeWindow(2) # stack: [1]
wm.getTopWindow() # -> 1
```
Example 2:
```text
Input:
["WindowManager", "getTopWindow", "openWindow", "openWindow", "openWindow", "getTopWindow", "closeWindow", "getTopWindow", "closeWindow", "getTopWindow"]
[[], [], [5], [7], [5], [], [5], [], [99], []]
Output: [null, -1, null, null, null, 5, null, 7, null, 7]
Explanation:
wm = WindowManager() # no windows open
wm.getTopWindow() # -> -1 (no windows open)
wm.openWindow(5) # stack: [5]
wm.openWindow(7) # stack: [7, 5]
wm.openWindow(5) # re-focus 5, stack: [5, 7]
wm.getTopWindow() # -> 5
wm.closeWindow(5) # stack: [7]
wm.getTopWindow() # -> 7
wm.closeWindow(99) # no-op (99 not open)
wm.getTopWindow() # -> 7
```
```hint Removing from the middle
A plain stack answers the top query in `O(1)`, but closing or re-focusing can hit a window at any depth. Think about which structure lets you find a window by id and cut it out of the middle of the order without scanning.
```
```hint Boundary cases
Watch the empty manager and re-focusing the window that is already on top.
```
### Constraints & Assumptions
- `1 <= windowId <= 10^6`
- At most `10^5` total calls across all operations.
- `closeWindow` on a window that is not open is a no-op.
- `getTopWindow` returns `-1` when no windows are open.
### What a Strong Answer Covers
- A structure that keeps the focus order and allows removal from any position, paired with a lookup from `windowId` to that position.
- `O(1)` (expected, with a hash map) for all three operations, with the reason each one is constant time.
- Re-opening an open window moves it to the top without leaving a second entry behind.
- Closing a window that is not open, and querying an empty manager.
- Space proportional to the number of open windows, with a closed window removed from every structure.
### Follow-up Questions
- A stack with lazy deletion also solves this. How does it work, and what does it give up compared with your design?
- Add Alt-Tab: cycle through windows in focus order without changing that order until the user releases the key. What changes in the design?
- Support `getWindowAt(k)`, the k-th window from the top. What does it cost with your structure, and how would you make it faster?
- How would you extend the manager to several virtual desktops, each with its own focus order, where windows can move between desktops?
Overview: Design a window manager that tracks open windows and their focus order, with open, close and top-window queries that all run in constant time. It tests choosing structures that support constant-time lookup and removal from the middle of an ordering, handling re-focus and no-op closes, and reasoning about worst-case versus amortized cost.