Alt and Tab Simulation - Problem
There are n windows open numbered from 1 to n. We want to simulate using Alt + Tab to navigate between the windows.
You are given an array windows which contains the initial order of the windows (the first element is at the top and the last one is at the bottom).
You are also given an array queries where for each query, the window queries[i] is brought to the top.
Return the final state of the array windows after all queries are processed.
Input & Output
Example 1 — Basic Alt+Tab Simulation
$
Input:
windows = [1,2,3], queries = [3,1,3]
›
Output:
[3,1,2]
💡 Note:
Start with [1,2,3]. Query 3 moves it to front: [3,1,2]. Query 1 moves it to front: [1,3,2]. Query 3 moves it to front: [3,1,2].
Example 2 — Single Query
$
Input:
windows = [1,2,3,4,5], queries = [4]
›
Output:
[4,1,2,3,5]
💡 Note:
Only window 4 is queried, so it moves to the front while others maintain their relative order.
Example 3 — Multiple Same Queries
$
Input:
windows = [2,1,3], queries = [1,1,1]
›
Output:
[1,2,3]
💡 Note:
Window 1 is brought to front on first query [1,2,3], subsequent queries of same window don't change the order.
Constraints
- 1 ≤ windows.length ≤ 103
- 1 ≤ queries.length ≤ 105
- 1 ≤ windows[i], queries[i] ≤ 106
- All elements in windows are unique
- All queries[i] exist in windows
Visualization
Tap to expand
Understanding the Visualization
1
Initial Windows
Windows in original stack order [1,2,3]
2
Process Queries
Each query brings a window to the front
3
Final State
Final window order after all queries
Key Takeaway
🎯 Key Insight: Process queries in reverse order to avoid expensive array shifting operations and achieve linear time complexity.
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code