Design Underground System - Watch the Algorithm Execute, Step by Step
Watching each pointer movement and node operation reveals how the linked list manages active check-ins efficiently and how route statistics are updated dynamically.
Step 1/10
·Active fill★Answer cell
initialize
create_node
Id:45
Station:Leyton
Time:3
connect
Id:45
Station:Leyton
Time:3
advance
Id:45
Station:Leyton
Time:3
traverse
Id:45
Station:Leyton
Time:3
compare
Id:45
Station:Leyton
Time:3
prune
Id:45
Station:Leyton
Time:3
Result:
Route data:
Leyton → Waterloo:[12, 1]
detach
Result:
Route data:
Leyton → Waterloo:[12, 1]
compare
Result:
Route data:
Leyton → Waterloo:[12, 1]
reconstruct
Result: 12
Key Takeaways
✓ Using a linked list to store active check-ins allows efficient insertion and removal without extra hash maps.
This insight is hard to see from code alone because the linked list pointer updates and traversal are subtle and visualizing them clarifies the data structure's dynamic nature.
✓ Traversal with prev and curr pointers is essential to find and remove the correct check-in node during check-out.
Seeing each pointer move and comparison step-by-step helps understand how nodes are unlinked safely without losing list integrity.
✓ Route travel times are aggregated incrementally, enabling constant-time average queries without re-traversing check-ins.
The trace shows how route_data updates after each trip, clarifying the separation of concerns between active check-ins and route statistics.
Practice
(1/5)
1. You need to design a data structure that supports inserting elements (including duplicates), removing one occurrence of an element, and retrieving a random element--all in average O(1) time. Which approach best guarantees these time complexities?
easy
A. Use a simple list and perform linear search for removals.
B. Use a balanced binary search tree to maintain elements and their counts.
C. Use a hash map to store element to indices mapping, combined with a list to store elements, updating indices on insert and remove.
D. Use a queue to track insertion order and a hash set for membership checks.
Solution
Step 1: Understand the operations required
Insert, remove (one occurrence), and getRandom must all be average O(1), even with duplicates.
Step 2: Evaluate approaches
A simple list with linear search (A) leads to O(n) removals. Balanced BST (C) has O(log n) operations. Queue and hash set (D) do not support random access efficiently. Hash map + list with index tracking (B) supports O(1) insert, remove, and getRandom by maintaining indices of duplicates.
Final Answer:
Option C -> Option C
Quick Check:
Hash map + list with index tracking enables O(1) average operations [OK]
Hint: Hash map + list with index tracking enables O(1) ops [OK]
Common Mistakes:
Assuming balanced BST can do O(1) getRandom
Using linear search for removal
Ignoring duplicates in data structure
2. Given the following code snippet for SnapshotArray, what is the output of snapshotArr.get(0, 0) after executing all lines shown?
Initially, data[0] = [(-1,0)]. After set(0,5), data[0] = [(-1,0),(0,5)] because snap_id=0. Then snap() increments snap_id to 1 and returns 0. Then set(0,6) appends (1,6) to data[0].
Step 2: Trace get(0,0)
For snap_id=0, bisect_right finds insertion point for (0,inf) in data[0] = [(-1,0),(0,5),(1,6)]. It returns index 2, so i=1. data[0][1] = (0,5), so get returns 5.
Final Answer:
Option A -> Option A
Quick Check:
Value at snap 0 is 5, not the later set 6 [OK]
Hint: Get returns value at or before snap_id using binary search [OK]
Common Mistakes:
Returning latest value ignoring snap_id
Off-by-one in bisect index
3. What is the space complexity of the optimal approach that copies a linked list with random pointers by interleaving copied nodes within the original list?
medium
A. O(1) because no extra data structures proportional to n are used
B. O(n) due to storing all copied nodes in a hash map
C. O(n) due to recursion stack in the copying process
D. O(n) because of auxiliary arrays used to track random pointers
Solution
Step 1: Identify auxiliary data structures
The optimal approach does not use hash maps or arrays; it weaves copied nodes into the original list.
Step 2: Analyze space usage
Only a few pointers are used; no extra space proportional to n is allocated, so space complexity is O(1).
Final Answer:
Option A -> Option A
Quick Check:
No hash maps or recursion stacks used [OK]
Hint: Interleaving nodes avoids extra space [OK]
Common Mistakes:
Assuming hash map is always needed, so O(n) space
Confusing recursion stack space with iterative approach
Thinking auxiliary arrays are used for random pointers
4. What is the time complexity of the divide and conquer approach to the Skyline Problem when given n buildings? Assume merging two skylines of total length m takes O(m) time.
medium
A. O(n²)
B. O(n)
C. O(n log n)
D. O(n * w) where w is the width of the skyline
Solution
Step 1: Identify divide and conquer recurrence
The algorithm splits buildings into halves recursively, then merges skylines in O(m) time where m is proportional to n.
Step 2: Solve recurrence and analyze merge cost
Recurrence is T(n) = 2T(n/2) + O(n), which solves to O(n log n). The width w does not affect complexity here.
Final Answer:
Option C -> Option C
Quick Check:
Divide and conquer with linear merge per level -> O(n log n) [OK]
5. Suppose the RangeModule must support intervals with negative coordinates and allow intervals to be added and removed multiple times (reusing intervals). Which modification to the optimal balanced BST approach is necessary to handle this correctly?
hard
A. Replace balanced BST with a segment tree to handle negative and overlapping intervals efficiently.
B. Use a hash map keyed by interval length instead of start points to speed up queries.
C. No modification needed; the existing balanced BST approach handles negative and reused intervals naturally.
D. Store intervals as half-open [start, end) and adjust bisect searches to handle negative values correctly.
Solution
Step 1: Consider negative coordinates impact
Bisect operations and interval storage must correctly handle negative values, which requires no change in data structure but careful handling of interval boundaries.
Step 2: Consider interval reuse
Intervals can be added and removed multiple times, so storing intervals as half-open [start, end) and merging intervals correctly remains essential.
Final Answer:
Option D -> Option D
Quick Check:
Adjust bisect and interval representation for negatives and reuse [OK]
Hint: Half-open intervals and bisect handle negatives and reuse correctly [OK]
Common Mistakes:
Thinking data structure must change to segment tree unnecessarily