Alt and Tab Simulation - Problem

Imagine you're working on multiple tasks on your computer with n windows open, numbered from 1 to n. Just like in real operating systems, you can use Alt+Tab to navigate between windows, which brings the selected window to the front (top of the stack).

You're given an array windows representing the initial order of windows, where the first element is at the top (most recently used) and the last element is at the bottom (least recently used).

You're also given an array queries where each queries[i] represents a window that gets brought to the top through Alt+Tab navigation.

Your task: Return the final state of the windows array after processing all queries, simulating the behavior of a window manager's Alt+Tab functionality.

Think of this as implementing the core logic behind your operating system's window switching mechanism!

Input & Output

example_1.py โ€” Basic Window Switching
$ Input: windows = [1,2,3,4], queries = [3,1,2]
โ€บ Output: [2,1,3,4]
๐Ÿ’ก Note: Initially: [1,2,3,4]. Query 3: move 3 to front โ†’ [3,1,2,4]. Query 1: move 1 to front โ†’ [1,3,2,4]. Query 2: move 2 to front โ†’ [2,1,3,4].
example_2.py โ€” Single Query
$ Input: windows = [5,3,1,4,2], queries = [4]
โ€บ Output: [4,5,3,1,2]
๐Ÿ’ก Note: Window 4 is at index 3. After bringing it to the front, the order becomes [4,5,3,1,2].
example_3.py โ€” Already at Front
$ Input: windows = [1,2,3], queries = [1,1,2]
โ€บ Output: [2,1,3]
๐Ÿ’ก Note: First query 1: already at front, no change. Second query 1: still at front. Query 2: move 2 to front โ†’ [2,1,3].

Visualization

Tap to expand
Window 1Front (Top)Window 2Window 3Alt+Tab SelectedWindow 4Back (Bottom)Move to FrontWindow 3Now at Front!Window 1Window 2Window 4Hash Map OptimizationWin 1 โ†’ 0Win 2 โ†’ 1Win 3 โ†’ 2Win 4 โ†’ 3O(1) Position Lookup!Instead of O(n) linear search
Understanding the Visualization
1
Initial Setup
Windows are stacked with most recent at the front
2
Alt+Tab Selection
User selects a window from the middle of the stack
3
Bring to Front
Selected window moves to the front, others shift back
4
Hash Map Optimization
Position tracking eliminates need to search through all windows
Key Takeaway
๐ŸŽฏ Key Insight: Using a hash map to track window positions transforms this from a searching problem into a direct access problem, mimicking how modern operating systems efficiently manage window switching!

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n ร— m)

While lookup is O(1), we still need O(n) time to shift elements for each query. Total: O(m) queries ร— O(n) shifting = O(nร—m)

n
2n
โœ“ Linear Growth
Space Complexity
O(n)

Hash map stores the position of each of the n windows

n
2n
โšก Linearithmic Space

Constraints

  • 1 โ‰ค n โ‰ค 103
  • 1 โ‰ค windows[i] โ‰ค n
  • All windows[i] are unique
  • 1 โ‰ค queries.length โ‰ค 103
  • 1 โ‰ค queries[i] โ‰ค n
  • queries[i] will always exist in windows
Asked in
Microsoft 35 Google 28 Amazon 22 Apple 18
23.4K Views
Medium Frequency
~15 min Avg. Time
856 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen