Bob Cavezza's Blog

I had a technical interview two days ago.

The algorithm I was asked to build was based around anagrams.

If you’re not familiar with what an anagram is, don’t worry, you’re not alone.

Two word are anagrams of one another if their letters can be rearranged to match one another. dan, and, nad, dna, adn are all anagrams of one another,

I had a hard time building this algorithm.

The reason is because I missed the key insight.

The key insight in creating this algorithm is that if you sort the strings, it will put the letters in alphabetical order, and you can compare the sorted strings to tell if the two strings areĀ anagrams.

‘dna’ sorted is ‘adn’
‘dan’ sorted is ‘adn
‘and’ sorted is ‘adn’

When you sort the strings, instead of doing a complicated comparison, you’re doing a straight equal comparison.

if sorted(string1) == sorted(string2)
anagram exists
else
not an anagram
end

Looking back, this looks like one of the simplest things I ever had to do.

However, I missed the key insight and it made everything that more difficult.

It makes me think, what key insight am I missing in everyday life that is making my life more difficult?

§888 · February 7, 2017 · Daily Digest · · [Print]

Leave a Reply