Interview Prep #1 : Hashmaps

  Data Structure Prep #1: Hashmap 

by Ashur Baroutta

    With internship and job interviews heavily reliant on testing proficiency in data structure and algorithm knowledge, I thought it wise to pursue learning more about some key concepts and solving more challenging problems. Hashmaps can be thought of as data structures that store key-value pairs, for every key there is a corresponding value.  The reason they're important to know is due to their time and space complexity. 

    Let's take a look at what that means in practice.  Our problem, this is one of several potential problems commonly given in internship interviews for top tech companies, states "given an integer array "nums", return true if any value appears at least twice in the array, otherwise return false if every element is distinct. Simply put, we're to check if a given array has a duplicate. 

    Many students, raises hand, try to brute force this problem with a nested loop solution. My nested loop code is as follows. 


    This does work, but it has massive limitations in regards to its time complexity as its O(n^2). Meaning it has to potentially iterate through a given array twice to find the duplicate as we are using nested loops (two loops). This is where hashmaps shine because with a hash map we can iterate through an array once and store values in the hashmap as we traverse the length of the array. Since we store the values as we go, we can check if the map has a duplicate value at the current stage of the traversal before we add it to the map, since if our map already contains the value then we are finished! My code submission using a hashmap is as follows.

    
    The logic is similar though the burden of computation is significantly lighter the larger our given array is. Hashmap usage is very valuable the more complex and larger a data set gets and I look forward to continuing exploring adding it to my toolkit as I solve complex problems in preparation for my upcoming internship interviews at top tech companies.



    

Comments

  1. Best of luck in your interviews. Keep pushing your capabilities and they'll undoubtedly see the value you will bring with you wherever you go.

    ReplyDelete
    Replies
    1. Thank you so much!

      I will continue pushing myself and improving my skill.

      I feel like breaking out of my review cycle was a huge step in the right direction, I'm learning alot more being hands on.

      Delete

Post a Comment

Popular posts from this blog

IP #6 : Reversing an Integer

IP#5 : LinkedList Removing Duplicates II