Another week has gone by, and I was lucky to partake in another technical assessment at a company that I really wanted to be a part of. I spent the weekend preparing myself on LeetCode and HackerRank, just like working on a jigsaw puzzle for fun. I was breezing through problems, I felt ready to conquer this assessment.
I had a working solution within 45 minutes of starting the assessment, and I felt good. But after I submitted and saw my score, I was flabbergasted. I came to realize that maybe the test cases were few and simple for a reason, it was up to me to test the functionality and efficiency of my solution. Once I recovered from the roller coaster ride experience, I sat down to reconsider my solution and how I could make it better. Of course, I also searched on Google for an explanation to no avail, so here’s my take.
The problem was given a number as a parameter, find how many steps it would take to get to that parameter. The kicker was you could either double the last number or add one to it. The test case was 18 with the desired solution to be 6 steps.
[0] = 1
[1] = 2
[2] = 4
[3] = 8
[4] = 9
[5] = 18
My solution was to build a hash where the key was the index and the value was the current sum, as that is how the problem was presented. If the number doubled is greater than the desired input, then the previous element must be reverted back and added 1. This was all contained in a while loop that is checking whether the values in the hash includes the given parameter. Then the solution would return the length of the keys in the hash.
function Solution(n) {
let ladder = {}
ladder[0] = 1
let x = 1
while(!Object.values(ladder).includes(n)) {
if (ladder[x-1]*2 > n) {
ladder[x-1] = ladder[x-2] + 1
ladder[x] = ladder[x-1]*2
} else {
ladder[x] = ladder[x-1]*2
}
x++
}
return Object.keys(ladder).length
}
While finalizing my solution minutes before submitting it, I wanted to see if the solution worked for some prime numbers around 18 like 17 and 19. A parameter set at 17 should return a length of 6, but instead I was getting 14. My code wasn’t working properly.
[0]=1
[1]=2
[2]=4
[3]=8
[4]=9
5=10
6=11
7=12
8=13
9=14
10=15
11=16
12=17
13=34
Not only was my code not working properly, but it was inserting an extra data point into the hash and distorting the answer. I came up with a simple fix by adding another condition to the if statement that tested whether the value would equal the parameter if one was added. Of course it worked for these two scenarios, but I wasn’t so sure if a max value was tested.
I am still currently working on a better solution that can handle these two randomly picked numbers. What I learned most from this assessment is that multiple test inputs should be tested before settling on a solution. It’s not enough to come up with a simple solution that handles a simple parameter. All in all, even though I do not have a background in Computer Science or advanced math, I need to start thinking like I do if I want to make a good impression on potential employers. Hopefully next week I will come up with a viable solution.
Top comments (0)