Show Buttons
Share On Facebook
Share On Twitter
Share On Google Plus
Share On Linkdin
Share On Reddit
Contact us
Hide Buttons

Create a bar chart from json

Problem :

Given a JSON data structure with key representing age and value representing the number of people with that age. For example,

{
  25: 55,
  26: 45,
  27: 10,
  28: 20,
  30: 1,
  31: 1,
  32: 3,
  33: 10,
  60: 10,
  64: 5,
  65: 5
}

startingAge = 20, endingAge = 65, ageInterval = 5. Create a graph which can plot in

Age range # People
[20, 25) 0
[25, 30) 130
[30, 35) 15
[55, 60) 15
[60, 65) 5

Logic :

  • Find the number of buckets required based on the start value, end value and age interval
  • Iterate over JSON one key at a time
    • Find out which bucket that key should belong
    • Add the value of it to the relevant bucket

Solution :

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *