- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
fri=st!
Admin
Just because his answer doesn't start with NumberCounterFactory I would have hired him on the spot.
Admin
"Surely my tester has the job of correcting my syntax? I don't have to be bothered with all that fiddling detail! I'm a java programming expert, I tell you!"
Admin
On the other hand, I've now used an online programming test tool twice, and each time the "correct" answer relied on a particular (arbitrary/implementation dependent) ordering of the output data that the serialization format does not require.
sigh
Admin
Admin
List ... = new Vector<Integer>(); ?!?
Did the candidate rewrite the data types or should Gareth be asked to leave as well? :)
Admin
https://www.tutorials.de/threads/modus-modalwert-berechnen.342907/#post-1775784
The original question here was for the modal value, but what the ..., median is a statistics function starting with an m as well.
Admin
I know this isn't a programming competition but here's my answer in c# :-)
var output = numbers.GroupBy(x => x).Select(x => new { key = x.Key, count = x.Count() }).OrderBy(x => x.count).Last().key;
Admin
Well, the task of finding a test which this candidate could pass is fairly simple : The reverse Turing test ! :D The candidate has shown to be able to pose as a dumb computer :)
Admin
This isn't a bad test, as far as they go. should be quick and easy for any programmer who knows algorithms at all, and will weed out over designers also.
Clearly, this calls for a complex MapReduce system...
Admin
OK here's one in Perl
foreach $num (@list) { $count{$num}++; }
$max = ( map { $->[0] } sort { $b->[1] <=> $a->[1] } map {[$, $count{$_} ]} keys %count )[0];
And they tell me Perl is unreadable :-)
Admin
Dam it DailyWTF ate my _'s
Admin
The real WTF is using Vector... (You can tell it's not Java 1.1 because of the Generics)
Admin
foue=rteenth!
Admin
The nature of the typos in this snippet is telling.
[
instead of{
- Understandable, you missed the shift key timing, it happens. But even the dumbest IDE will tell you there's a non-matching pair somewhere.!:
instead of1;
- Why are you even pressing shift here? You just let go of it in order to type "+=" properly!I'd like to see this guy's resume. Is it also riddled with heads-down "nothing I've typed can be wrong" finger-waggling?
Admin
I find it more amusing to think that they wrote this while coding on a whiteboard.
Admin
The best answer of course would have been
.... if (one > two) { if (one > three) { ....
Admin
Little shorter C#.
var output = numbers.GroupBy(x => x).OrderBy(x => x.Count()).Last().Key;
Or with a less LINQ...
Dictionary<int, int> numberCounts = new Dictionary<int, int>(); foreach(int number in numbers) { if(numberCounts.TryGetValue(number, out int currentCount)) numberCounts[number] = currentCount + 1; else numberCounts[number] = 1; }
int maxCount = 0; List<int> highestNumbers = new List<int>(); foreach(var numberCount in numberCounts) { if(numberCount.Value > maxCount) highestNumbers = new List<int> { numberCount.Key }; else if (numberCount.Value == maxCount) highestNumbers.Add(numberCount.Key); }
// highestNumbers now contains all the numbers with the highest count and will contain multiple values in the event of a tie.
Admin
You don't need to go full Schwartzian there.
my @ordered = sort { $count{$b} <=> $count{$a} } keys %count;
Then take the first element of @ordered. Simple. Please don't make Perl look harder than it is. :)
Admin
At one point, we had all potential candidates take an online FizzBuzz test with a time limit of 45 minutes. The online tool had some test cases built in, so candidates could see if their code worked or not. More than 90% of applicants could not even come close to a valid solution. I should have saved some of their submissions and submitted them here... you would have balked at the solution that a "senior architect" concocted. This really opened my eyes to the reality of the software engineering world; for every one decent software engineer, there are 9 more people who should never be touching your code.
Admin
Very easy.. hand the list to my brother, he will let me know.
Admin
A lot of inefficient one liners here (especially with the sorts). I would just do it manually. Run through once, keep a map and track of the max. But that's more lines of code you say. Well not at all. You just make it a function and now it's a one liner. I have to ask, what happens when more than one have the same max count?
Admin
As far as this test is concerned, my answer would've been
Integer max=7;
or whatever correct Java syntax is.
Admin
Do things manually is premature optimization. Do it the more "readable" way first, measure it, identify if performance is unacceptable, and THEN optimize it.
Admin
One liners arent always "readable"... cramming things into one line for sake of being on one line is often detrimental to readability.
Admin
Even shorter in Kotlin:
(The question mark ensures it returns null if the list is empty. If several numbers appear the same number of times, this returns the first. — It's possible I'm over-thinking this, of course!)
I was going to write an imperative solution, too, but realised that it would basically do the same as the above…
I love Kotlin :-)
Admin
Re: perl
Oh come on :-) You can count them and find the max at the same time, for lower algorithm complexity.
A little uglier if you want to avoid the "undefined" warnings:
Admin
Or simply do this in Haskell:
mostOccs xs = maximumBy (comparing (\ x -> length $ filter (==x) xs)) xs
Of course, better to do it in point free style. How else would you impress your interviewer?
mostOccsPointFree = maximumBy =<< comparing . (length .) . flip (filter . (==))
Or maybe avoid evaluating the list again for every element and just sort twice. Also nice that this is in point free style:
mostOccsFast = head . last . sortOn length . group . sort
Admin
I'll do one in PHP - just to see if that's enough to get the comment held for moderation.
$tally = array_count_values($numbers); $mode = array_keys($tally, max($tally))[0];
Addendum 2018-08-08 06:31: Nope, guess not.
Admin
Looks like output from a Markov chain.
Admin
Still seems like there’s way too much over-engineering here. Try this code on for size:
// find the most common item int mostCommonItem = 7;
Done.
Admin
SELECT max(num) AS Commonest FROM Numbers
Admin
The correct answer would have been: return 7;
returns the correct value for the challenge, only one line of code AND readable
Admin
This was my solution as well, with the addition of:
}
Admin
This way you only loop through the map once. An alternative would be to loop through once to find the maximum frequency and then loop through again to gather only the matches. Either way, it's probably not too far off from what all of these library calls to sort/filter are doing, except without the overhead penalty.
Admin
You certainly made it less readable.
Admin
@Tim - I wouldn't go near you with a bar pole if you presented that LINQ monstrosity as answer. There's a reason why we used to have a developer we called Mr Linq and there's a reason he no longer works.
Admin
I definitely agree, do the simplest that works first so you have a point of reference for any optimisation. Then optimise it if there's an obvious way it's it's just one function. What's better anyway? That copy paste one liner of multiple functions everywhere or a single function (get most common keys) that you can use anywhere and know performs well? Sure you can put the one liner in the signle function, but it's just one function with an easy optimisation, so might as well just do it. Many people misunderstand the no premature optimisation line. It doesn't mean skip obvious opportunities for optimisation upfront if they're simple, quick and easy to apply with no significant burden on maintenance. Contrary to popular belief the little bits can add up as well.
Readability though is massively subjective. This isn't the best example as the task is simple enough even the short versions aren't doing too much complex. There are cases though where you're saying do this, that and the other to an array and while it might only be a few function calls you have to track things going on that are way more complex than if you just did it straight up.
If you do really care for performance in a fundamental way then it's worse because you really do have to think a lot about what's happening with those functions, not just what goes in and how it comes out. You might want to imagine ambient and general performance isn't important but then try thinking like that when you code depends on a battery to keep it running (for example) or you're trying to port something to a pi.
To be fair though, I've also seen people do a lot of stupid things in the name of performance, that didn't actually make things faster (or failed to address the root problem, like caching SQL results before schema/query optimisation).
Admin
Javascript, the ugly version:
a = [ 5, 14, 6, 7, 7, 7, 20, 10, 10 ] b = { }, a.forEach ((x) => { b[x] = (b[x]|0)+1;}) c = Object.keys (b).map ((x) => b[x]) d = Math.max.apply (Math, c) e = c.indexOf (d) f = Object.keys (b)[e] console.log ('Number ' + f + ' appears more frequently')
A "preview comment" button would be a nice addition to TDWTF ;)
Addendum 2018-08-11 10:12: TDWTF ate my newlines :'(
See https://gist.github.com/
Addendum 2018-08-11 10:12: See https://gist.github.com/matonga/0f013f4ce279e8cdb48440203f820911 :-) (oops)
Admin
import java.io.; import java.util.;
class Solution {
public static int mostCommon; public static int maxCount;
public static void main(String[] args) { List<Integer> numbers = new Vector<Integer>();
System.out.println("Most common value is " + mostCommon +"."); }
}
Admin
Does not work, if there are two or more numbers with same count.
Admin
LINQ one-liner getting job done could be:
var output = numbers.GroupBy(n => n).Where(n => n.Count() == numbers.GroupBy(m => m).Max(m => m.Count()));
but still, it probably can be done more elegantly.