Dynamic Median with Additions and Deletions
Updated
Problem statement
Process operations on an integer multiset and return one result for every median query.
[1, x]adds one occurrence ofx.[2, x]removes one occurrence ofx, or does nothing whenxis absent.[3]queries the current median.- For a nonempty multiset, return the median as a reduced
numerator/denominatorstring with a positive denominator. For an empty multiset, return the literal stringnull.
Function
dynamicMedians(operations: int[][]) → String[]Examples
Example 1
operations = [[1,5],[1,1],[3],[1,9],[3],[2,5],[3],[2,1],[2,9],[3]]return = ["3/1","5/1","5/1","null"]The queried multisets are {1,5}, {1,5,9}, {1,9}, and the empty multiset.
Example 2
operations = [[1,2],[1,2],[1,8],[3],[2,2],[3],[2,2],[3],[2,2],[3]]return = ["2/1","5/1","8/1","8/1"]Each deletion removes only one duplicate occurrence. The final deletion of absent value 2 is a no-op.
Constraints
1 <= operations.length <= 200000.- Every operation is exactly
[1, x],[2, x], or[3]. -1000000000 <= x <= 1000000000.
More ByteDance Onsite Interview questions
- Search a Row- and Column-Sorted MatrixONSITE INTERVIEW · Seen Nov 2026
- Alien DictionaryPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Lexicographically Smallest Topological OrderPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Implement Multi-Head AttentionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Serialize and Deserialize a String Key-Value StoreONSITE INTERVIEW · Seen Oct 2026
- Binary Tree Maximum Path SumONSITE INTERVIEW · Seen Oct 2026
- Merge Overlapping IntervalsONSITE INTERVIEW · Seen Oct 2026
- Tic-Tac-Toe Move WinnersONSITE INTERVIEW · Seen Oct 2026
- Kth Largest Element without a HeapONSITE INTERVIEW · Seen Oct 2026
- Least Recently Used CachePHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
Similar questions at other companies
- 15-Minute Chat Session TrackerOpenAI · PHONE SCREEN · ONSITE INTERVIEW
- Top K Upvoted Songs by ArtistAmazon · ONSITE INTERVIEW
- LRU Cache with TTLAutodesk · ONSITE INTERVIEW
- Tiered Expiring Item StoreHudson River Trading · PHONE SCREEN · ONSITE INTERVIEW
- Thread-Safe LRU Cache with TTLLinkedIn · ONSITE INTERVIEW
- Top K Frequent Elements After Each Stream UpdateSalesforce · ONSITE INTERVIEW
- Limit Order Book Matching EngineCitadel · PHONE SCREEN · ONSITE INTERVIEW
- Task Scheduler with DependenciesScale AI · ONSITE INTERVIEW
- Trade Volume LeaderboardBloomberg LP · PHONE SCREEN · ONSITE INTERVIEW
- Design Authentication ManagerOracle · ONSITE INTERVIEW