HackerRank
Published: 2019-05-21 03:51:24

---
title: HackerRank
published: true
description: HackerRank Challenges
tags: 
---
Hello, all. In this post, I want to talk a little bit about what has transpired since my last post.

My group project was successful, but I do wish I could've played more of a role. I was tasked with the HTML of one page of the project, but not the database stuff that I really wanted to learn. I hope to use the database stuff from my group project as an example for the Clean Exchange. So far, I have looked at the HTML that's already made and played around with the Bootstrap that one of the contributors used. I've never used Bootstrap before then, but it seems like a very easy way to add style to your web page.

I started HackerRank challenges today along with challenges from other websites. The reason for this is because I had a technical interview last week and the interviewer recommended I learn hash sets and hash maps. Little did I know that hash maps in Java are actually similar to dictionaries in Python, which I did learn in a freshman university class. The interviewer immediately lost interest in hiring me after I admitted I didn't know what hash maps and hash sets were, but he let me move on to show my code.

On top of that, he recommended I take time to talk out loud about my thinking process before diving in to coding and trying things out. In school, it was never really stressed to think about the things that can go wrong. I've always just typed whatever and tested it to see if it would work, but my interviewer wasn't satisfied with my method of coding because he mentioned that it takes up company resources. I used too many loops and he said that using loops when they weren't necessary was wasteful and recommended a much easier solution to the problem he tasked me with.

I've also had a technical interview online recently that I absolutely failed at because I didn't understand the questions and I was supposed to use a programming language I haven't used in about 2 years. On top of my inexperience I realized I had in both interviews, I also was very nervous with time constraints and being in front of a live human, and it made me sloppy.

Because of my failure at the technical interviews, I want to sharpen my programming skills by taking on challenges online. So far, I'm working on the warm up assignments in HackerRank. Most have been easy, but one caught my attention. The "Pairs" warm-up had me looking through an array of elements and output how many pairs there are that had a given difference between the two. It sounded easy, but when I submitted my code, I realized that I failed the later test cases. The reason was because there was a time-out. Apparently, my code took too long.

I looked online and realized that HackerRank times out if your code takes longer than expected. I looked further online and found that this can occur if there are too many loops, among other reasons, which is exactly something my interviewer warned me about. At that point, I thought now might be a good time to heed what my interviewer recommended and learn about hash sets and hash maps. With that fresh in my mind, I thought about how I could make my code more efficient so it doesn't time out. I decided instead of having 2 for loops, I could just put everything in the array in one HashMap and then have 1 for loop. In the for loop, I would look for the key that is equal to the difference between the current value I'm looking at and the difference value. 

I'm honestly shocked at how important something I learned in a freshman class came in handy for technical interviews and making my code more efficient. Because of how important this is, I will post my solution to the challenge below. Next time, I hope to discuss further any progress I've made with the Clean Exchange, any other personal projects I start, and the HackerRank challenges. I hope to find more ways to use hash maps and hash sets to make my code more efficient and to make me a more appealing candidate in interviews. I also hope to practice thinking about things before testing them out.

        HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();

        for(int i = 0; i < arr.length; i++)
            map.merge(arr[i], 1, Integer::sum);

        int count = 0;
        for(int i = 0; i < arr.length; i++){
            if(map.containsKey(arr[i]-k))
                count += map.get(arr[i]-k);
        }
        return count;