Live data from Hacker News

LINQ Ruined My Favorite Interview Question

scottchamberlin.tumblr.com

191–200 of 218 posts

Re: LINQ Ruined My Favorite Interview Question

#191
post #82

Earlier quoted context omitted.

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)

That's almost identical to what I came up with, but yours is better...I didn't know about the mapValues function. def mostCommon(str: String, num: Int) = { str.split(" ").groupBy { s => s} .map { case (k,v) => k -> v.length }.toList .sortBy { _._2 }.reverse.take(num).map { _._1 } }

That works as well. On the other hand, I'm not a fan of these huge one-liners (including the one I posted). I split mine up a bit and posted it here: https://coderwall.com/p/ha-c2w

Re: LINQ Ruined My Favorite Interview Question

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

but seriously, if you're writing c# and not using LINQ all the time You don't sound arrogant, but rather sound naive. I agree that someone who recruits surely should have known about and have experienced LINQ significantly by now, but the notion that you should be using it "all the time" is absolute nonsense. I avoid LINQ. I encourage others to avoid LINQ. It is almost always a sign of bad code. LINQ is syntactical s…

I get what you're saying, I understand it, and I agree with the pitfalls. But I really really really don't agree that one should avoid LINQ. Sure if you don't understand how it works, and how it impact the performance, you should use it less. But don't try and drag the language down because you find it difficult. LINQ is easily one of the best things that have happened to a mainstream language in a long time, and if used right it produce highly maintainable code, that is very easy to read and understand. A lot of data manipulation really is done on sets, and LINQ makes it a breeze to do.

Re: LINQ Ruined My Favorite Interview Question

#193
post #136

Earlier quoted context omitted.

Because of an older version of python?!

If they're stuck on a version of software that hasn't been supported in 5 years for bugs or security with a set of features that was originally released nearly 10 years ago, it means that there are some culture issues in the workplace that will most likely drive me insane. Therefore, I am unlikely to take the job.

Most companies are not startups, installing software as soon as new releases get available.

Just to give another example, our consulting company still gets requests for projects to be deployed against Java 1.4!

Re: LINQ Ruined My Favorite Interview Question

#194
post #20

Earlier quoted context omitted.

LINQ is not a magic bullet. It has some serious pitfalls. I'd like to aee people using the right tools for the job.

LINQ is a great mechanism for expressing complex operations in a succinct manner. Sure, LINQ may not be a magic bullet and can be abused, but you know the old adage about premature optimisation. Perhaps you could elaborate on "It has some serious pitfalls.".

I didn't write the post you responded to, but I can tell you of the main pit fall I have encountered. By far the worst is evaluating the same enumerable again and again by mistake. It don't show up in unittests because they use small data sets, but as soon as you do any kind of load test or put it into production it's painfully slow. The solution is often very simple, but you really have to get dirt on your hands before you recognize where it is a problem.

Re: LINQ Ruined My Favorite Interview Question

#195
post #20

Earlier quoted context omitted.

LINQ is not a magic bullet. It has some serious pitfalls. I'd like to aee people using the right tools for the job.

LINQ is a great mechanism for expressing complex operations in a succinct manner. Sure, LINQ may not be a magic bullet and can be abused, but you know the old adage about premature optimisation. Perhaps you could elaborate on "It has some serious pitfalls.".

See https://news.ycombinator.com/item?id=6026280

Re: LINQ Ruined My Favorite Interview Question

#196
post #185

So, aside from the Clojure, Mathematica, Python, Ruby, Bourne Shell, Haskell, and Scala solutions posted in the other comments, all of which are simpler than the C++, C#, and JS solutions, presented here with some minor cleanups: (take 10 (reverse (sort-by (comp first rest) (frequencies (string/split ... #"\+s")))) ; llambda Clojure // haakon Scala s.split(' ').groupBy(identity).mapValues(_.size).toList.sortBy(-_._2)…

Small C++11 improvement:

     string s,f;
     map M;
     set> S;
     while(cin >> s) {
             M[s]++;
             int x=M[s];
             if(x>1) S.erase(make_pair(x-1,s));
             S.insert(make_pair(x,s));
     }
     auto it=S.rbegin();
     int topK=10;
     while(topK-- && (it!=S.rend())) {
             cout secondfirst
Surely, it could even be more improved with help from lambdas and algorithms.

Re: LINQ Ruined My Favorite Interview Question

#197
post #17
post #4

LINQ tends to get thought of as "database syntax sugar", but it's way more than that. It's C#'s version of the lazy collection operations you find in most functional languages, just given friendlier sqlish names. (IE, "Select" instead of "map" and "Where" instead of "filter") I almost feel a bit gross when I have to write a "foreach" loop at this point, because there's almost always an equivalent way to do it in LINQ…

I almost feel a bit gross when I have to write a "foreach" loop at this point Agreed. I've transitioned to spending most of my time in JS, and wherever I can I use .map(), but the chaining it's not quite the same as LINQ. Someday I intend to write a library of Array addons to provide GroupBy and so on, but I can't imagine it'll be super efficient.

This may be what you're looking for. I think it was developed my MS specifically for this reason:

https://github.com/Reactive-Extensions/RxJS

Re: LINQ Ruined My Favorite Interview Question

#198
post #146

I'm not sure that language features making code more succinct really ruin the question. The C# isn't even that succinct compared to doing a similar thing in other popular languages. For example, in Haskell: topTenWords :: String -> [String] topTenWords = take 10 . map fst . sortBy (flip (comparing snd)) . map (\l -> (head l, length l)) . group . sort . words

flip (comparing whatever) is a neat trick! I'll have to remember that.

For \l -> (head l, length l) I tend to use head * * * length (without the spaces between those stars).

Re: LINQ Ruined My Favorite Interview Question

#199
post #17

Earlier quoted context omitted.

I almost feel a bit gross when I have to write a "foreach" loop at this point Agreed. I've transitioned to spending most of my time in JS, and wherever I can I use .map(), but the chaining it's not quite the same as LINQ. Someday I intend to write a library of Array addons to provide GroupBy and so on, but I can't imagine it'll be super efficient.

In Underscore, you can do something like: _([ ... ]).map( ... ).filter( ... ).value() It's not exactly extending the native array...but there are far fewer side effects to doing it this way.

The problem with this is that it's doing multiple iterations. LINQ on the other hand builds up an expression tree that is "compiled" into a single loop when `ToList()` (or some other method which gets the results) is eventually called.

Re: LINQ Ruined My Favorite Interview Question

#200
post #188

Earlier quoted context omitted.

string s,f; map M; set > S; while(cin >> s) { M[s]++; int x=M[s]; if(x>1) S.erase(make_pair(x-1,s)); S.insert(make_pair(x,s)); } set >::reverse_iterator it=S.rbegin(); int topK=10; while(topK-- && (it!=S.rend())) { cout second first

Not bad. I think it would be a little simpler and faster with: while (cin >> s) M[s]++; for (map ::iterator i = M.begin(); i != M.end(); i++) { S.insert(make_pair(i->second, i->first)); } But maybe there's a downside to that approach that isn't obvious to me?

If you move what while (topK--) into the map loop , it becomes an online code for topK whereas what you wrote is an offline . If you want offline then pushing it into a priority_queue and then popping it out would be much faster.
Post reply on HN