Live data from Hacker News

LINQ Ruined My Favorite Interview Question

scottchamberlin.tumblr.com

81–90 of 218 posts

Re: LINQ Ruined My Favorite Interview Question

#82
post #8

I hate to be the bearer of bad news, but I think there may be even simpler solutions to this problem: (take 10 (reverse (sort-by (comp first rest) (frequencies (string/split ... #"\+s")))) The above is a Clojure one-liner example that I believe satisfies the original problem. So while LINQ may have simplified from the C-language family solutions he had seen, it's clearly possible to take it one step further with the…

Since we're already on the JVM, here's my solution for Scala:

def top10(s: String) = s.split(' ').groupBy(identity).mapValues(_.size).toList.sortBy(-_._2).take(10).map(_._1)

Re: LINQ Ruined My Favorite Interview Question

#83
post #7

/me wonders are candidates allowed to chose their favorite language? man bash | tr '[:upper:] ' '[:lower:]\n' | sed '/^$/d' | sort | uniq -c | sort -rn | head | awk '{ print $2 }' | fmt or do you only hire windows coders?

Perhaps, if you did it in Powershell?

Re: LINQ Ruined My Favorite Interview Question

#85
post #68
post #31

Earlier quoted context omitted.

Without imports: d = {} for word in s1.split(' '): try: d[word] += 1 except KeyError: d[word] = 1 print [(x, d[x]) for x in sorted(d, key=d.get, reverse=True)][:10]

I'm a newbie, but the question seemed approachable so I went for it. This is what I came up with. (Python, btw.) def top_ten(s): words = s.split(' ') word_list = set(words) return sorted(word_list, key=lambda x: words.count(x))[:10] The question didn't ask for word counts, so I didn't see the need for a dictionary. I'd appreciate any advice on my solution. I'd be thrilled if I'm not too far off from being capable of…

Nice. Just a little fixing and polishing:

    def top_ten(s):
        words = s.split()
        return sorted(set(words), key=words.count, reverse=True)[:10]
(to get the most common words instead of the least).

Re: LINQ Ruined My Favorite Interview Question

#86
post #8

I hate to be the bearer of bad news, but I think there may be even simpler solutions to this problem: (take 10 (reverse (sort-by (comp first rest) (frequencies (string/split ... #"\+s")))) The above is a Clojure one-liner example that I believe satisfies the original problem. So while LINQ may have simplified from the C-language family solutions he had seen, it's clearly possible to take it one step further with the…

The OP blog-post doesn't really represent it succinctly in C#. This is the most succinct way I could think of:

  var top = (from w in text.Split(' ') 
             group w by w into g 
             orderby g.Count() descending 
             select g.Key).Take(10);

Re: LINQ Ruined My Favorite Interview Question

#88
post #48

Earlier quoted context omitted.

Underscore has groupBy and some of the others you may be looking for.

Yes.. I do understand why extending the base Array object in JS is deeply frowned upon but at the same time I often wish that these sort of extensions were on the Array object. It feels clunky to use them otherwise.

You should check out Sugar, by far my favourite standard JS lib to use in any complex project: http://sugarjs.com/

Re: LINQ Ruined My Favorite Interview Question

#89
post #42
post #25

Earlier quoted context omitted.

It looks like collections is part of the standard library: http://docs.python.org/2/library/collections.html . I think this disqualifies your JS solution, as long as doStuff isn't some fancy nodejs core module I missed.

I think the point still stands though, when you think of it as an interview question. The point is to get to the bottom of how the candidate would process such a query themselves, not to test their knowledge of the core libraries of their favourite language. I feel like that's what the post author meant when he said that LINQ had "ruined" the question- because it kind of does the same thing.

Isn't it kind of axiomatic that any question that provides for a concise and measurable solution will eventually be refactored into a library? It seems like kind of a weakness in these kinds of "programmer" questions, that they all regress to trivia. Why not an open-ended business problem and/or deliverables-type question? I'm sure that would speak more to the daily job, where I've never heard of someone being tasked with "OK, find the top 10 words by frequency in a random Wikipedia article."

Re: LINQ Ruined My Favorite Interview Question

#90
post #13

I hate to be the arrogant know it all on Hacker News, but seriously, if you're writing c# and not using LINQ all the time, you need to catch up. I'm tired of seeing people answer interview questions with anything _other_ than LINQ.

I disagree, but for different reasons than the other commenter. LINQ is great for transformations that are intuitively described by the verbs LINQ provides. If your transformation is most easily understood as a process of mapping filtering grouping selecting etc, then by all means. But trying to pigeonhole every iterative process into LINQ is a recipe for code that's hard to decipher. As always its a question of how does one communicate a process most effectively. Succinctness for its own sake is a fools errand. Clarity is key, and sometimes that means foregoing LINQ for standard iteration.
Post reply on HN