The PROBLEM with CodeWars

The PROBLEM with CodeWars

A friend recently introduced me to codewars.com, if your not familiar with it, it is a site where you can solve different problems using code and test your proficiency or learn from others. It is actually quite fun, and somewhat similar to questions usually asked in technical interviews.

I am still on the "easy" questions as I have not ranked up high enough for harder questions to emerge. Once you have a solution and submit it, you are allowed to see other solutions different developers have come up with. You can choose to do so in many different languages, I was interested to see how JavaScript would look so I clicked that. There are thousands of different variations of solutions, however, participants can "Up Vote" those solutions they feel are either "Clever" or "Best Practices". The basic concept being that perhaps, you as a developer, can learn better ways of developing solutions or addressing issues. I've noticed an albeit subjective pattern amongst the "cleverest" and "best practiced" solutions. They seem to be the shortest code possible.

Let me give a demonstration to illustrate this. Here is a sample question that I got (again I know it's on the easy side, haven't gotten to the good stuff yet :))

"You will be given an array (which will have a length of at least 3, but could be very large) containing integers. The array is either entirely comprised of odd integers or entirely comprised of even integers except for a single integer N. Write a method that takes the array as an argument and returns N.

For example: [2, 4, 0, 100, 4, 11, 2602, 36] Should return: 11

[160, 3, 1719, 19, 11, 13, -21] Should return: 160"

You can write some unit tests to help if you enjoy TDD (which I do), so I started off writing my tests and then came up with

function findOutlier(arr){
	var need,value;
	integers.slice(0,3).filter((v)=>{ return v % 2 == 0}).length >= 2 ? need = 1: need = 0;
	for(var x = 0;x< arr.length;x++){
		if(Math.abs(arr[x] % 2) == need){
			value = arr[x];
			break;
		}
	}
	return value;
}
 
  

Passed my tests and everything worked great. Then looking at others solutions almost universally up-voted for being clever and the best practice was this

function findOutlier(int){
  var even = int.filter(a=>a%2==0);
  var odd = int.filter(a=>a%2!==0);
  return even.length==1? even[0] : odd[0];
}
 
  

Like I said I looked through probably 20 other solutions each had maybe 2 or 3 upvotes where this one had 130, universally declared the best.

I was the shortest amount of characters to accomplish the task for sure and clever but for whatever reason it seems people discount the good old trusty for loop. Its old and boring I guess. There were others that weren't quite the same but were similar to mine with maybe 1 or 2 up-votes.

Although nice and short and elegant, the problem I had with it was it was parsing through the array COMPLETELY, well and TWICE.

In other-words, although looking quite cooler than a for loop, it was not as performant, so I created an array of 10,000, with the outlier at position 5000. In other-words the outlier was right in the middle. Obviously the closer to the beginning the more performant my function would be but figured putting it in the middle was a good random sampling. Then ran the two functions 50 times and averaged the results.

My function came out to an average of 1.5 milliseconds as it obviously stops parsing through the array once it has found its outlier.

The best practices function averaged 2.4 milliseconds as it spent a lot more time parsing through the array.

Now perhaps this is merely based on the JavaScript community. Perhaps I will go do the C# or Java problems and see if the community is more concerned about performance than code length. It is possible, especially since there are less JavaScript developers that have as much experience in optimization that this is isolated to this community.

However it would be nice if something was automatically run to help people see the performance of any given solution (Reminder: Submit this suggestion to CodeWars).

Now, one could make an argument that the brevity of code ITSELF increases performance by requiring less time to pass over the wire, however with minification and uglification procedures I don't think most would sway that way currently.

The moral is, CodeWars is fun, however sometimes we shouldn't underestimate the good old trusted, simple, ugly, easy to read and debug, For Loop.

What are your thoughts?



Brant, thanks for sharing!

Like
Reply

Well... there's no MAJOR performance issue in terms of time complexity. It's very negligible. The overall time complexity is O(2*n) which is pretty much o(n). The space complexity is also O(2*n) = O(n) since you're creating two arrays every time the function gets called. I think it's more interesting to think about how you can avoid o(n) memory using 'ugly' loops. Great job on the elegant solution!

Like
Reply

It seems interesting Brant. I will go through this on weekend and post my comment. Thanks for sharing.

Like
Reply

To view or add a comment, sign in

Others also viewed

Explore content categories