4 ms·
How’s the old interview question go? Using the programming language of your choice, sort the strings in this file and remove duplicates. You have 30 minutes.
by bluedino 9y ago
How’s the old interview question go?
Using the programming language of your choice, sort the strings in this file and remove duplicates. You have 30 minutes.
sort foo | uniq
- NullPrefix 9y agoClearly a suboptimal solution. That pipe isn't free. /s sort -u foo
- my123 9y agoSeems to be too little UNIXy. Modern tools sometimes not only do one job. (the z flag might have been kept out of tar too for example) :P
- emilfihlman 9y agoWith piping you can leverage multiprocessing for free. Using sort -u alone might actually not be as fast as sort | uniq -u.
- viraptor 9y agoIf you're using gnu coreutils: sort -u foo.
- amelius 9y agoAnd what if the strings are separated by spaces instead of newlines?
- flerchin 9y agosed foo -e 's/ /\n/g' | sort | uniq
- amelius 9y agoYeah, but what if the strings I wanted to sort contain newlines?
- flerchin 9y agoYou haven't specified a delimiter.
- jws 9y agoYou (sir|madam), are a feature creep. If it weren’t for people like you and their “real requirements” software would be easy. By the time you say “unicode” and “collation” the programmer is going to be in serious trouble. Fortunately the programmer can lawyer the problem. The original requirements don’t specify what the strings are sorted by. I choose “position in original source” file and complete the task with a NOP.
- diegoperini 9y agoecho "Input file already sorted."
- stepvhen 9y agoprotip: tr ' ' '\n' < foo | sort | uniq
- innagadadavida 9y agoIf you want to preserve order, this is how you uniq with awk: awk '!x[$0]++'
- deleted 9y ago[deleted]
- flatline 9y agoI always give props to people who give a solution like this because sometimes that is the right thing to do and it acknowledges that we are dealing with a toy problem with a ready solution. And I think that’s what the interviewer in this post failed to do. But in reality we may have a not-dissimilar problem that doeasnt deal with strings and is just a bit more complicated so you can’t just take a bash command or genetic function off the shelf and have it work. You’re going to have to write some code, it’s going to take a little thought, and your solution can’t be O(n^2) when we throw ten million elements at it. I’ve worked with engineers who are competent programmers but just can’t handle these problems when they come up in practice. If the job actually requires it, I don’t know of a better way in the context of an interview to determine if a person is that type of programmer than with a few toy problems and a solution in code. You may miss some good people because the format is suboptimal, but you will weed out the people who just don’t have the theoretical and practical background to solve this particular type of problem.
- new299 9y agoI'm not certain that it's true that you "will weed out the people" who can't solve algorithmic problems. There are a relatively small number of algorithmic problems, that are simplest enough to be asked and solved in an interview context. People now study these types of problems for months when preparing for interviews... I think there are better ways of doing things than the standard X interviews of 1 hour. One method that works well is hiring people for short term contracts (few days) and seeing how they work with the team on real problems.
- hn_throwaway_99 9y ago> One method that works well is hiring people for short term contracts (few days) and seeing how they work with the team on real problems. The problem is that most really great people, who are in high demand, won't put up with this because they know they can get someone else to hire them with less hassle. Your approach ends up weeding out your best candidates.
- 9y ago
- deleted 9y ago[deleted]
- emilfihlman 9y agoBroken. uniq alone removes only adjacent duplicates. You need uniq -u to remove all duplicates.
- benchaney 9y agoThis is not a concern for already sorted lists.
- JadeNB 9y ago> > sort foo | uniq > Broken. uniq alone removes only adjacent duplicates. You need uniq -u to remove all duplicates. As benchaney (https://news.ycombinator.com/item?id=17060154 https://news.ycombinator.com/item?id=17060154) points out, the duplicates will be adjacent after `sort`.
- sireat 9y agoDoesn't have to be Bash either, Python is pretty good for this too: sorted(set(foo.strip.split(' '))) #although we need to open the file first just to be on the safe side Perl's not too shabby either for this kind of stuff :)