• 14 (unregistered)

    fri=st!

  • Raj (unregistered)

    Just because his answer doesn't start with NumberCounterFactory I would have hired him on the spot.

  • Little Bobby Tables (unregistered)

    "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!"

  • (nodebb)

    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

  • someone (unregistered)

    a team player and collaborator is more important than someone who can recite the documentation for your library of choice from memory The problem there is a poorly-designed test, not with asking for code samples.

  • Martin (unregistered)

    List ... = new Vector<Integer>(); ?!?

    Did the candidate rewrite the data types or should Gareth be asked to leave as well? :)

  • (nodebb)

    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.

  • Tim (unregistered)

    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;

  • Rigongia (unregistered)

    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 :)

  • Kashim (unregistered)

    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...

  • JG (unregistered)

    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 :-)

  • JG (unregistered) in reply to JG

    Dam it DailyWTF ate my _'s

  • Henning (unregistered)

    The real WTF is using Vector... (You can tell it's not Java 1.1 because of the Generics)

  • NickJ (unregistered)

    foue=rteenth!

  • Chronomium (unregistered)

    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.
    • "foue=rteen" - This looks like hitting "e" instead of "r", then hitting "=" instead of backspace. So you caught an error, and then assumed your correction was correct.
    • !: instead of 1; - 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?

  • Anonymous') OR 1=1; DROP TABLE wtf; -- (unregistered) in reply to Chronomium

    I find it more amusing to think that they wrote this while coding on a whiteboard.

  • Javahead (unregistered)

    The best answer of course would have been

    int one, two, three, fouer, five, six, seven, eight, nine, ten;
    int eleven, twelve, thirteen, fourteen, fifteen, sixteen, eighteen, nineteen, twenty;
    
    // find most common item
    for(Integer num : numbers){
    if(num == 1 ){
      one += 1;
    } 
      else if(num == 2 ){
        two += 1;
      }
      else if(num == 3 ){
        three += 1;
      }
      else if(num == 4 ){
        fouer += 1;
      }
      else if(num == 5 ){
        five += 1;
      }
    

    .... if (one > two) { if (one > three) { ....

  • Ghandi (unregistered) in reply to Tim

    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.

  • Randal L. Schwartz (google)

    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. :)

  • Jebus (unregistered)

    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.

  • (nodebb)

    Very easy.. hand the list to my brother, he will let me know.

  • isthisunique (unregistered)

    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?

  • itseasy (unregistered)

    As far as this test is concerned, my answer would've been

    Integer max=7;

    or whatever correct Java syntax is.

  • Anonymous Coward (unregistered) in reply to isthisunique

    Do things manually is premature optimization. Do it the more "readable" way first, measure it, identify if performance is unacceptable, and THEN optimize it.

  • Anonymous Coward (unregistered) in reply to Anonymous Coward

    One liners arent always "readable"... cramming things into one line for sake of being on one line is often detrimental to readability.

  • gidds (unregistered)

    Even shorter in Kotlin:

    val commonest = numbers.groupBy{ it }.maxBy{ it.value.size }?.key
    

    (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 :-)

  • (nodebb) in reply to JG

    Re: perl

    Oh come on :-) You can count them and find the max at the same time, for lower algorithm complexity.

    $count{$max} < ++$count{$_} and $max= $_ for @numbers;
    

    A little uglier if you want to avoid the "undefined" warnings:

    ($count{$max//''}//0) < ++$count{$_} and $max= $_ for @numbers;
    
  • J. (unregistered)

    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

  • (nodebb)

    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.

  • karol (unregistered)

    Looks like output from a Markov chain.

  • A Nana Mouse (unregistered)

    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.

  • most readable one-liner (unregistered)

    SELECT max(num) AS Commonest FROM Numbers

  • ikariw (unregistered) in reply to gidds

    The correct answer would have been: return 7;

    returns the correct value for the challenge, only one line of code AND readable

  • Zenith (unregistered) in reply to isthisunique

    This was my solution as well, with the addition of:

    Int32 Integer_Frequent = 0;
    List List_Frequent = new List();
    foreach (Integer_Key in Dictionary_Map.Keys)
    {
      if (Dictionary_Map[Integer_Key] > Integer_Frequent)
      {
        Integer_Frequent = Dictionary_Map[Integer_Key];
        List.Clear();
      }
      if (Dictionary_Map[Integer_Key] == Integer_Frequent)
      {
        List_Frequent.Add(Integer_Key);
      }
    }
    return List_Frequent;
    

    }

  • Zenith (unregistered) in reply to Zenith

    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.

  • comment-498320 (unregistered) in reply to JG

    You certainly made it less readable.

  • Ex-EMIS Dev (unregistered)

    @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.

  • isthisunique (unregistered)

    Do it the more "readable" way first, measure it, identify if performance is unacceptable, and THEN optimize it.

    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).

  • Matías Moreno (google)

    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)

  • Michel (unregistered)

    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>();

    Random r = new Random();
    int low = 1;
    int high = 6;
    
    for (int i=0;i<1000;i++){
    	numbers.add(r.nextInt(high-low)+low);
    }
    
    // find most common item
    HashMap<Integer,Integer> map = new HashMap<Integer,Integer>();
    
    for (int i = 0;i<numbers.size();i++) {
    	
    	int key = numbers.get(i);
    
    	if (map.containsKey(key)) {
    		int newCount = map.get(key)+1;
    		map.replace(key,newCount);
    		if (newCount > maxCount) {
    			maxCount = newCount;
    			mostCommon = key;
    		}
    	}
    	else {
    		map.put(key, 1);
    	}
    }
    

    System.out.println("Most common value is " + mostCommon +"."); }
    }

Leave a comment on “This Interview Doesn't Count”

Log In or post as a guest

Replying to comment #:

« Return to Article