Another lesson learned
Last week I wrote about a lesson I learned that taught me to know your data. The reason for this is to know how you will need to manipulate that data so you can transform it into a more workable piece of data. Thanks to this lesson, I take time to digest the data structure or structures I am facing.
This ‘know your data’ lesson came in handy in my most recent technical interview. I was to fetch from an API with multiple keys, the values to these keys were objects, whose keys were arrays. After some time sitting with the data structure I had an idea on how to complete my task.
My task for this was to iterate through the data and pull back the key that had the most items in its array. After getting into the first object, I was able to loop over the object and check the length of the array against a variable initialized at ‘0’. If the length was longer, I would reset the variable to be the length, otherwise, I would continue through the object and check other lengths; eventually, this would yield me the longest array length.
Now that I had my max array length, I filtered the object keys whose value was equal to the max variable. This last bit of logic returned the key I was originally trying to pull based on the problem. My interview said I had achieved the goal of the exercise, but I had made an unnecessary loop. He then clued me in on the fact that I could have set a new variable outside my loop and updated it with the key I was looking for as I already had access to the key in question. This was a powerful lesson, know when you have access to the data you are looking for. I have begun to work with this and refactor my code for any steps that may not be needed.