Hacker News new | ask | show | jobs
by mmorett 4666 days ago
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."

Here's your anagrams checker tough guy:

  static boolean anagram(String word1, String word2) {
    char[] chars1 = word1.toCharArray();
    char[] chars2 = word2.toCharArray();
    Arrays.sort(chars1);
    Arrays.sort(chars2);
    return Arrays.equals(chars1, chars2);
  }
I was speaking in the abstract, but you made it personal.

Would you like me to build a search engine next?

2 comments

Okay, now make it faster.

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.
Then please accept my apologies. I enjoyed the anagram thing as it forced me into looking into it.