Interview Query

Automatic Histogram

Have you seen this question before?

Given a list of integers called dataset, write a function called automatic_histogram to automatically generate a dictionary representing a histogram of the data set with x bins uniformly distributed over the values.

Note: You should not include any bins that have zero values in them in your dictionary.

Note: Do NOT use numpy or pandas.



x = 3
dataset = [1,2,2,3,4,5]


automatic_histogram(dataset, x) -> {'1-2': 3, '3-4': 2, '5': 1}


x = 3 so our dictionary should have three entries

  1. The values 1 and 2 are mapped to the first index of the histogram, so we count the values [1,2,2] of dataset as {'1-2': 3}
  2. The values 3 and 4 are mapped to the second index of the histogram, so we count [3,4] as {'3-4': 2}
  3. The value 5 is mapped to the third and final index of the histogram, so we count the single value of [5] in the dataset as {'5': 1}
Next question: Amateur Performance
Python 3.9.6
Loading editor
Use Shift + Enter to run code