DEV Community

Cover image for I just failed my coding test...heres the passing solution
Samejima
Samejima

Posted on

I just failed my coding test...heres the passing solution

So I just failed my coding test due to running out of time. It was one of those codility tests only 70 minutes and 3 different problems, I passed on the first 2 questions however I ran out of time with the 3rd problem.

Here is the question that gave me issues

Image description

Image description

initially I assumed we would need to use the sliding window technique to get the contiguous 'x' and measure from there and I hacked at that solution for about 20 minutes but in the final 5 minutes out of pure desperation and need i came up with another solution that i did not get to fully flesh out in the official test.

The idea of the solution is in 3 simple broken up steps,

  1. separate the contiguous x
  2. sort by length
  3. decrement from your budget

this solution is magnitudes simpler to comprehend and a LOT easier to read. I may not have gotten the ability to benefit from coming up with this solution but hopefully someone reading this in the future will.

here it is in python

def solution(S, B):
    # Implement your solution here
    count = 0

    #sliding window technique needed
    arr = []
    temparr = []
    for index in S:
        if index == "x":
            temparr.append(index)
        else:
            arr.append(len(temparr))
            temparr = []
    arr = sorted(arr)
    index = len(arr)-1
    while B > 0 or index == 0:
      if B >= arr[index]+1:
        B -= arr[index]+1
        count += arr[index]
      index -= 1
    return count
Enter fullscreen mode Exit fullscreen mode

Top comments (4)

Collapse
 
ben profile image
Ben Halpern

Good luck, you'll get it next time

Collapse
 
sreno77 profile image
Scott Reno

I hate coding tests like this for interviews. They don't test that you'd be good for the job at all. A much better coding test is to give the interviewee a day or two to complete a mini project and show how you would actual perform under work conditions.

Collapse
 
samejima profile image
Samejima

i dont hate coding tests since they are faster than a mini project but youre right about them not assessing how well you would do on the job.

Collapse
 
anitaolsen profile image
Anita Olsen

I am so sorry to hear that! You will nail it next time! ✨