Practice
Solution
Step 1: Understand the problem requirements
The data structure must support add, remove, and query operations on intervals efficiently.Step 2: Evaluate approaches
Brute force scanning is O(n) per operation, which is inefficient. DP is not suitable for dynamic interval updates. Greedy merging only on query delays merging and leads to inefficient queries. Balanced BST with interval merging maintains sorted intervals and merges overlapping ones, supporting O(log n) operations.Final Answer:
Option A -> Option AQuick Check:
Balanced BST with merging -> O(log n) per operation [OK]
- Thinking DP or greedy solves dynamic interval updates efficiently
snapshotArr.get(0, 0) after executing all lines shown?
snapshotArr = SnapshotArray(3)
snapshotArr.set(0, 5)
snap_id = snapshotArr.snap()
snapshotArr.set(0, 6)
print(snapshotArr.get(0, 0))
Solution
Step 1: Trace set and snap operations
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 AQuick Check:
Value at snap 0 is 5, not the later set 6 [OK]
- Returning latest value ignoring snap_id
- Off-by-one in bisect index
get and put operations in an optimal LRU Cache implementation using a hash map and a doubly linked list with capacity n?Solution
Step 1: Analyze get operation
Hash map provides O(1) access to node; doubly linked list allows O(1) removal and insertion to update usage order.Step 2: Analyze put operation
Insertion involves hash map update and linked list insertion/removal, all O(1). Eviction removes tail node in O(1).Final Answer:
Option B -> Option BQuick Check:
Both get and put run in constant time using combined data structures [OK]
- Assuming list removal is O(n)
- Confusing amortized with worst-case
- Thinking linked list needs balancing
Solution
Step 1: Understand tweet reuse impact
Allowing duplicate tweetIds means multiple posts with same id but different times must be treated as distinct tweets.Step 2: Adjust data structures
Each post creates a new TweetNode with unique timestamp; heap and linked lists handle duplicates naturally by time ordering.Final Answer:
Option A -> Option AQuick Check:
Multiple nodes with same tweetId but different times must be stored separately [OK]
- Trying to deduplicate tweets in feed incorrectly
- Rejecting duplicate posts unnecessarily
Solution
Step 1: Understand removal challenges
Heaps do not support efficient arbitrary removals, so direct removal breaks heap properties.Step 2: Use lazy deletion
Maintain a delayed deletion map to mark elements for removal and prune them lazily during heap operations, preserving heap structure and efficiency.Step 3: Avoid costly rebuilds
Rebuilding heaps on every removal is inefficient; ignoring removals is incorrect.Final Answer:
Option D -> Option DQuick Check:
Lazy deletion with pruning maintains correctness and efficiency [OK]
- Rebuilding heaps on every removal
- Ignoring removals breaks correctness
