Downsample Dense Time Series For Charts While Keeping Spikes
Last updated: November 29, 2026
Quick Overview
A chart can only render a few hundred points but the query returned hundreds of thousands. Write a function that reduces sorted `(x, y)` points to at most `max_points` while keeping the first and last points and every bucket's minimum and maximum so outliers are still visible.
Sigma Computing
November 29, 20260
0
0 solved
A chart can only render a few hundred points but the query returned hundreds of thousands. Write a function that reduces sorted `(x, y)` points to at most `max_points` while keeping the first and last points and every bucket's minimum and maximum so outliers are still visible.
Sigma charts run against warehouse tables that can be huge, and naive sampling hides exactly the spikes an analyst is looking for. The interviewer checks bucket math, ordering inside a bucket, and whether you know when to push the reduction into SQL.
What the Interviewer Expects
- Explain why every-kth-point sampling loses peaks
- Choose bucket count from the point budget (two points per bucket plus endpoints)
- Keep min and max per bucket in original x order so the line does not zigzag backwards
- Guarantee the output never exceeds the budget
- Discuss doing the same reduction in the warehouse with window functions or GROUP BY bucket
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- How does Largest-Triangle-Three-Buckets compare, and when would you choose it?
- How would you write this as a SQL query so only a few hundred rows leave the warehouse?
- What happens with multiple series, or with unevenly spaced timestamps?
- How would you re-downsample on zoom without refetching everything?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
Input is sorted by x. Output is a subset of the input points, sorted by x, of length at most max_points, including the first and last point. Any point that is the min or max within its bucket surviv...
High-Level Design
Reserve two slots for the endpoints. Split the inner points into (max_points - 2) // 2 equal-width index buckets. From each bucket keep the min-y and max-y points, emitted in their original order (i...