Write a function to merge overlapping intervals, and explain how you would optimize your solution for real-time data streams.