Implement Terminal Tabs and Split Panels
Company: Warp
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Quick Answer: This question evaluates the ability to design and implement hierarchical data structures, model tree-based panel layouts, and perform precise serialization/deserialization of nested UI state.
Constraints
- 0 <= len(operations) <= 10000
- Each operation is either ('new_tab',) or ('split', tab_id, panel_id, direction)
- Every split operation targets an existing leaf panel in the specified tab, and direction is either 'H' or 'V'
- Panel IDs are assigned globally starting from 1
Examples
Input: [('new_tab',), ('split', 0, 1, 'H'), ('split', 0, 2, 'V'), ('new_tab',), ('split', 1, 6, 'V')]
Expected Output: {'serialized': [{'tab_id': 0, 'layout': {'type': 'split', 'panel_id': 1, 'direction': 'H', 'children': [{'type': 'split', 'panel_id': 2, 'direction': 'V', 'children': [{'type': 'leaf', 'panel_id': 4}, {'type': 'leaf', 'panel_id': 5}]}, {'type': 'leaf', 'panel_id': 3}]}}, {'tab_id': 1, 'layout': {'type': 'split', 'panel_id': 6, 'direction': 'V', 'children': [{'type': 'leaf', 'panel_id': 7}, {'type': 'leaf', 'panel_id': 8}]}}], 'round_trip_equal': True}
Explanation: Tab 0 starts with panel 1, which is split horizontally into panels 2 and 3. Panel 2 is then split vertically into panels 4 and 5. A second tab starts with panel 6, which is split vertically into panels 7 and 8. After serialization and deserialization, the structure must remain identical.
Input: [('new_tab',)]
Expected Output: {'serialized': [{'tab_id': 0, 'layout': {'type': 'leaf', 'panel_id': 1}}], 'round_trip_equal': True}
Explanation: This edge case creates a terminal with a single tab and no splits, so the layout is just one leaf panel.
Input: []
Expected Output: {'serialized': [], 'round_trip_equal': True}
Explanation: This edge case has no operations at all, so the terminal remains empty and the round-trip still succeeds.
Input: [('new_tab',), ('split', 0, 1, 'V'), ('split', 0, 3, 'H'), ('split', 0, 4, 'V')]
Expected Output: {'serialized': [{'tab_id': 0, 'layout': {'type': 'split', 'panel_id': 1, 'direction': 'V', 'children': [{'type': 'leaf', 'panel_id': 2}, {'type': 'split', 'panel_id': 3, 'direction': 'H', 'children': [{'type': 'split', 'panel_id': 4, 'direction': 'V', 'children': [{'type': 'leaf', 'panel_id': 6}, {'type': 'leaf', 'panel_id': 7}]}, {'type': 'leaf', 'panel_id': 5}]}]}}], 'round_trip_equal': True}
Explanation: This test checks deeper nesting in a single tab. Panel 1 splits into 2 and 3, then panel 3 splits into 4 and 5, and finally panel 4 splits into 6 and 7.
Hints
- A panel layout is naturally a tree: leaves are unsplit panels, and internal nodes are split panels with two children.
- Keep a map from panel_id to the panel node so splitting a panel does not require traversing the whole tree every time.