DEV Community

indiejesus2
indiejesus2

Posted on

Prepping for the Technical

It finally happened! I had my first interview last week. I was giddy with excitement and anxious to know what was ahead. If it all went well, I would move on to phase 2 of the interview process which would be a coding test .

While I had no official word whether I have moved on, I wanted to brush up on some skills so I could be prepared for the test. I knew that once I started the test, I would have an hour time limit, a little daunting. So this past weekend I tried some coding challenges out on one of the many software development prep websites out there. This particular one featured an interview prep kit of problems.

As I wanted to ease my way into the process, I chose to start with the "easier" challenges. Or at least that’s what they called them. I’ll joking aside, the first major point I would like to make is to be sure to read the instructions carefully. I started making a function that returned an unnecessary value and took a lot of valuable time.

The first challenge presented a scenario where you would have to match socks in this case, basically taking an array Of numbers and pairing them off, then delivering how many pairs existed in the array. My immediate thought was a for loop would have to be set up that would cycle through the array for any potential matches. Instead of the variable beginning at 0, the first item of the index, the variable would begin at 1, as the conditional statement would be comparing the proceeding items with that first item. Obviously, if the condition is not met, the variable would increase by one.

function twinsies(n, ar) {
    let x = 0
    let pairs = 0
    while (x < n) {
        for (let i = 1; i < ar.length; i++) {    
            if (ar[0] == ar[i]) {
                ar.splice(i, 1)
                ar.splice(0, 1)
                pairs++
            }
        }
        x+=1
    }
    return pairs
}
Enter fullscreen mode Exit fullscreen mode

If the condition was true, then the second part of the pair would be spliced from the array followed by the first item of the array. Then a counter initiated before the loop would increase by one. It was important to splice the first item after the found item to assure the correct item was being spliced from the array. If the beginning of the array was removed, then the items would be moved up.

This was enough to pass the preliminary tests, but it wasn’t acceptable for the remaining sample test cases. In particular, if the first item in the array does not have a corresponding pair in the array. My solution could be stuck in an endless loop as the first item in the array would never be removed from it’s lack of matches. I had to alter my code to handle this scenario.

My solution was to check if the array contained the first item before the loop was started. This included creating a separate array that sliced out the beginning, and if it does not include the first item, the item would be spliced from the array. This was enough to pass the remaining tests.

function twinsies(n, ar) {
    let x = 0
    let pairs = 0
    while (x < n) {
        let rest = ar.slice(1)
        if (!rest.includes(ar[0])) {
            ar.splice(0, 1)            
        } else {
            for (let i = 1; i < ar.length; i++) {    
                if (ar[0] == ar[i]) {
                    ar.splice(i, 1)
                    ar.splice(0, 1)
                    pairs++
                }
            }
        }
        x+=1
    }
    return pairs
}
Enter fullscreen mode Exit fullscreen mode

I know this wasn’t the most creative nor efficient solution to the given scenario. I didn’t mention before, but the function would be given two parameters, the second parameter being the array and the first parameter given was the amount of items in the array. As I do not come from a computer science or math background, I was unsure how useful the length of the array would be.
I also had to set up a while loop to assure the function wouldn’t become an endless loop, which is where the first parameter came into use. A second counter was set up that would increase after the if statement completed its task, and once the counter was the same as the first parameter, the function would break. Again, it helped solve the problem but it performed unnecessary tasks if the array became empty. It keeps me up at night.

I only managed to complete a few warm-up challenges and haven’t even begun the actual prep work. If I need a break from networking, sending out resumes and tinkering with my personal portfolio, I’m sure I’ll make my way back to the interview prep work. Hopefully I will hear back soon about moving onto the technical aspect for the potential employer and I can let you know what that test was like. Fingers crossed!

Discussion (0)