Phil, the point is who gives a flying f in production about anagrams. Noone has to deliver a meaningful abstraction in < an hour (or 15 minutes as a "simple" example of "puzzle" testing), and interviewing for that is truly ridiculous.
You and others might find it meaningful but after two decades of development I really have to question those who do.
Like you - I've never needed to solve an anagram in production.
I think many are missing the point. In an interview situation, the anagram problem gets the person to write some basic, naive code (ie, proves that can track loops) which I think we would both agree is reasonable.
Then the interviewer gets you to look at how to improve it. Hopefully you'll notice that iterating over both strings isn't very efficient, and reason about ways to improve that.
It's that reasoning process that is important, not the knowledge of how to write an anagram solver.
To be fair, though - most programmers should be able to solve it with a couple of hints.
If anything, not being a real problem is a feature. Very few candidates will have ever actually had to solve anagram problems, so most candidates will be equally unfamiliar with the question, making it a fair measure of the candidate's ability to solve simple problems they haven't seen before.
Here's an example of an interview question that doesn't have this feature: "How would you serialize a tree?" The Lisp programmers will get that one right away. Even people with a passing familiarity with XML will get it right away. You don't know if they would have been able to figure it out themselves, all you know is that they've seen serialized trees before.
Tree serialization seems reasonable - basically you end up looking at tree walking algorithms and some recursion, right? (I can't remember the costs & benefits of depth first vs breadth first traversal, but I'm sure I'd work something out).
OTOH, I was once asked to write a Sudoku solver in an interview. I'd never really solved a Sudoku myself at the time (although at least I knew how it worked), so I had to try and work out an approach to solving the puzzle as well as how to write the code. I found that pretty tough.
The purpose of a question like this, and of the classic FizzBuzz question, is to be a lightweight test of whether a candidate can actually write code or not. That's all. It provides a negative signal if they fall completely to get anywhere close, and nothing more.
There are alternatives, you could ask them to explain a piece of code they wrote on their own before the interview, any piece of code would do. I suppose that could be gamed which is why most companies opt for FizzBuzz.
Personally, once I started thinking about these kinds of questions like this, I had less issue with them. Interviewing people who failed to write FizzBuzz opened my eyes too.
Since a reply link isn't available for spacemanaki's comment, I'll stick it here:
"The purpose of a question like this, and of the classic FizzBuzz question, is to be a lightweight test of whether a candidate can actually write code or not. That's all. It provides a negative signal if they fall completely to get anywhere close, and nothing more."
I disagree. Vehemently. Provide me an email address and I'll mail you tons of code I've written. You cannot, cannot conclude with any accuracy that the inability to code up a method/function to check an anagram means that the person cannot write code. What you can conclude is that they cannot write up a method/function to check an anagram.
You're making an assumption. If a = b and b = c, then a = c. That works in math class but not here. The test is not one of syntax or knowledge of the language but of knowledge of a specific problem. Some here have used the word "trick". I don't even need to go there. Once you have the answer to this anagram exercise, you can back into why it's a good question, what it supposedly shows, etc. At that point you're justifying the question and defending it.
But you missed the entire point of the exercise: you're supposedly screening candidates for knowledge of a language and how they might have used it in their past experience, but letting it all come down to whether they solve your specific problem on the spot with all of Pamela's concerns about stress, etc.
It's easier to ask about an anagram than to ask about a candidate's past and what they did on their projects. If not an anagram, there are others. There will always be "solve this now" rather than "tell me about..." That's just the game.
> You cannot, cannot conclude with any accuracy that the inability to code up a method/function to check an anagram means that the person cannot write code. What you can conclude is that they cannot write up a method/function to check an anagram.
If you can't check an anagram given the definition of an anagram and the programming language of your choice, what problems can you solve? It's not a hard question and if you can't come up with a correct solution at all, I dare say you lack the ability to program.
So, I understand and am incredibly sympathetic to the stress problem some folks have. I also occasionally freeze up when put on the spot, though I fortunately tend to recover after a minute or so of verbal stumbling.
That said, hiring quality engineers is HARD, and often ends up coming down to a numbers game. If I've got 100 applicants to go through and know (from past experience) that over 80% of them couldn't code their way out of a wet paper bag, then I need short, deterministic interview techniques that will identify those candidates who are actually worth my time. Thus: fizzbuzz. I've met lots of folks who talk a great game, have all kinds of experience listed on their resume, and yet somehow can't write up a linked list or fizzbuzz solution in any language.
So, I accept that there will be some false negatives in order to more quickly narrow the focus down to those few rare folks that can actually code. It sucks: it isn't entirely fair to folks who struggle with stress/nerves, but I need a 5-15 minute litmus test that can be done remotely and I haven't found anything else more effective.
Short of wasting everyone's time with take-home projects (which I often employ in later stages of the interview process), what other options are out there?
That's the $64,000 question. If I knew the answer, that would be my startup. It's a tough problem. It almost makes one beg for some sort of one-off certification (bar exam?) so we can put that to rest for ever after. I'll go out on a limb and say that most lawyers, especially the senior ones, probably couldn't pass the bar exam again. Developers have a mini bar exam every time they interview (hence the studying).
That said, there's one thing you wrote I took interest in: "I've met lots of folks who talk a great game, have all kinds of experience listed on their resume, and yet somehow can't write up a linked list..."
Umm...that would be me. I've never written a linked list. Guilty as charged. Java has a LinkedList class and I've never bothered to look at the implementation nor have I ever been curious enough to (re)write one from scratch.
I wish you would have said "...and yet somehow don't know when to use a linked list." That moves it from an academic exercise mostly applicable to CS students who might have written one 6 months ago, to a meaningful real world scenario and more than legitimate. Choosing the appropriate data structure, knowing when and why, is both realistic and important. I don't know how a candidate can bullshit their way thru a discussion of data structures where they can articulate, accurately, which one to use, when, and why.
> I've never written a linked list. Guilty as charged. Java has a LinkedList class and I've never bothered to look at the implementation nor have I ever been curious enough to (re)write one from scratch.
Good! You're right: the JDK's implementation is perfectly acceptable, and you'd be crazy to write your own. I certainly haven't, either.
But if you have any CS education, I'll bet you know what a linked list is at a high level. And I can probably verbally walk you through turning that into a "spec" that's good enough for a basic whiteboard implementation of one that supports a couple of basic operations. It's (hopefully) just another fizzbuzz: a simple problem to demonstrate that you can code something/anything. The overwhelming majority of applicants can't.
I'll ask you a slightly different question then: when is it appropriate to use a linked list? In which of those cases might it still be inappropriate and why?
These things can be memorized of course but I find that if one has a reasonable understanding of the underlying machine and knows how to implement a range of data structures and algorithms the answer to these questions (for most of the less specialized data structures) becomes quite straightforward. Whether that is relevant very much depends on the job being interviewed for however.
I really have to question those who find the question so difficult they would rather rationalize why they shouldn't have to answer it or handwave about how they can't possibly solve even simple programming problems in anywhere less than an hour.
HN seems to lack reply links in some messages so I need to answer you here.
"If you can't check an anagram given the definition of an anagram and the programming language of your choice, what problems can you solve? It's not a hard question and if you can't come up with a correct solution at all, I dare say you lack the ability to program."
I'm not trying to be obnoxious, but your answer is not optimal. Which is not a big deal, and it would give us a chance to talk about ways of optimizing code. Sooner or later we'd figure out if you knew what a hash table is and how to use it (no real points off if you never really got, under the pressure of an interview, the hash table solution to this problem), how you would go about profiling code, how you might solve this if they weren't strings but social security numbers, and so on. It's a launching off point to more interesting discussions. At least that's how I'd use it in an interview. Before long we'd be talking about a, I dunno, search engine or something.
Lol!! You're killing me. I need to work tomorrow and you're gonna have me up all night thinking how to optimize this thing. It's only 5 lines of code.
Hashtable, huh? I don't like them because they're synchronized, but let's use a HashMap instead. Regardless, I'm stumped.
Let me think out loud. You'd probably want me to stick two entries in there with the words being the values. I suspect the keys are irrelevant. But I need the values themselves sorted prior to being placed in the map. Lets assume I stick the char arrays in the map. I still can't see how the map helps. Unless, I abandon the HashMap and go with a TreeMap instead. That negates the need to explicitly sort and maybe that's the time savings, but....
My head hurts. :-)
I don't know. I'd have to code this out and experiment a bit. But I like the push. You're forcing me to dig deeper.
PS. I can't build a search engine.
EDIT: A quick search of the net yields at least one answer (don't know if this works):
"We can solve this problem in O(n) time by using hashtable. That is, create a hashtable which record the times that the characters a-Z appear in S1 and create another hashtable for S2. If the two hashtables have the same content, then the strings are identical. This solution requires O(n) time to create two hashtables and compare them, and O(52) = O(1) space to store the hashtable."
I'm so disgusted. I was way off. Not even close. I wasn't even thinking in this direction. Does anyone have the number to that truck driving school?
I'm pretty sure the sort solution would be an order of magnitude faster on any reasonably sized string. Big O's are Big O's : they are irrelevant when n is small, and for anagrams we're talking something like n < 45.
Reply links take longer to generate on HN as comment threads get deeper in order to deter people from losing their temper and flaming each other. In your case I would say that didn't quite work. My comment was not meant personally but as a general statement. If you had kept your composure and waited for the reply link to show up instead of flying off the handle about it and replying to an unrelated comment, perhaps we would be having a more productive conversation right now.
You and others might find it meaningful but after two decades of development I really have to question those who do.