Downsample Song Audio Into Waveform Peaks for Rendering
Last updated: November 29, 2026
Quick Overview
Write a function that turns an array of audio samples into a fixed number of (min, max) peak pairs for drawing a waveform at a given pixel width, then extend it with a precomputed multi-resolution pyramid so zooming a long song stays fast.
Suno
November 29, 20260
0
0 solved
Write a function that turns an array of audio samples into a fixed number of (min, max) peak pairs for drawing a waveform at a given pixel width, then extend it with a precomputed multi-resolution pyramid so zooming a long song stays fast.
Suno Studio and the song player draw waveforms for multi-minute stereo songs and stems in the browser. The interviewer checks correct bucket math, why min/max beats averaging for audio, and how you precompute levels so zoom does not rescan millions of samples.
What the Interviewer Expects
- Split samples evenly into width buckets with integer math
- Keep min and max per bucket, not the mean
- Handle more pixels than samples
- Build a pyramid where each level halves the previous
- Pick the level closest to the zoom and finish with a small rescan
- Mention doing the work off the main thread
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 would you render while audio is still being generated and streamed?
- Where would you compute peaks: server, Web Worker, or WebGL?
- How would you handle stereo and very quiet stems?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
waveform_peaks(samples, width) returns width pairs; pixel i covers samples [i*n//width, (i+1)*n//width). A 3 minute song at 48 kHz has about 8.6 million samples per channel, so rescanning on eve...
High-Level Design
Level 0 stores min/max per block of base samples (for example 256). Each higher level merges pairs from the level below. To render a zoom where one pixel spans s samples, pick the deepest level whos...