Maintain a Streaming Median
Company: Cognitiv
Role: Machine Learning Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Quick Answer: This question evaluates understanding of streaming algorithms and dynamic data structures for maintaining order statistics, along with analysis of time complexity and memory trade-offs.
Constraints
- Inputs are Python literals matching the function signature.
- Return a deterministic exact-match value.
Examples
Input: ([["add",2],["add",3],["add",1],["median"],["add",4],["median"]],)
Expected Output: [2, 2.5]
Explanation: Medians after odd and even counts.
Input: ([["median"],["add",5],["median"]],)
Expected Output: [None, 5]
Explanation: Empty stream median is None.
Hints
- Clarify edge cases before coding.
- Keep the return value deterministic.