Building a histogram from raw data
Question
Write code that turns a list of numbers into a histogram.
Short answer
Choose bins, then count in one pass. Fix the range and number of bins (width ), set up zero counters, and for each increment bin , clamping the top edge into the last bin and handling out-of-range values. This is time and space; discuss bin-width choice and normalising counts to a density.
Adapted from Joshi, Denson & Downes, Quant Job Interview Questions and Answers, Q5.7, p.158; question and answer rewritten by SelfMadeQuant.com.
See the hint and the full worked solution in the question bank (free account).
Open the worked solution