Palindromes, Base 16 & Roman Numerals

Sitting at a Street Bean – coffee shop right next to my work. I started at Code Fellows a little less then a year ago thinking this will be my long term gig. Since then I’ve met a lot of energetic software engineers, passionate management… essentially people who care about changing people’s lives for the greater good… this is my last week here. Next week I start at Nike.

In addition to teaching iOS, I have spent the time covering data structures and helping students get ready for interviews. Learning how to “code” is only half the battle… I am sure most of my friends would agree.

What I’ve noticed is that typical interview questions evolve around Linked Lists, Palindromes, Base Conversion, Binary Search trees, etc. You might never use these data structures in production, but they come up quite often on interviews and thus are important to know if you want to get a job. In this article I will cover 3 questions and write implementation in Swift 3. Linked Lists topic has been covered pretty extensively already by other developers… I won’t bother.

So what do you say if we begin with palindromes? Let’s do it.

A palindrome is a word, phrase, number, or other sequence of characters which reads the same backward or forward. – Wiki

Wow word is a palindrome. Mom word is a palindrome. Pop word is a palindrome. You get the point. Writing implementation is straight forward as it only requires string reversal and then comparison between the original input and the reversed output. Time complexity of this solution is O(n).

The above implementation will check if a word is a palindrome. Sometimes you’ll get a challenge to return all palindromes from an array of million words. To accomplish that, we will only need to write one additional function that iterates over each word and checks for palindrome. Pretty straight forward.

Next up is Base Conversion. Specifically, we will be converting from decimal number system to hexidecimal number system. To be perfectly honest, I had no idea what “Base Conversion” meant. I don’t have traditional CS degree. I still remember texting my friend Branton asking about it.

There are a couple of ways to tackle this challenge. One way involves UnicodeScalar conversion (I started with that) and is a bit more involved. The above implementation is the by far the cleanest and can be easily refactored to account for base 2 conversion, etc – credit to Branton.

Let’s talk about Roman numerals. Typical question evolves around converting Roman numerals to decimal number system and can be a bit daunting at first. The challenge is to differentiate numbers such as “IV” and “VI” for example. Before writing implementation, let me yet again turn to Wikipedia for definition.

The numeric system represented by Roman numerals originated in ancient Rome and remained the usual way of writing numbers throughout Europe well into the Late Middle Ages. – Wiki

Generally speaking, I found solving these little problems quite interesting.

Huge thank you to all my Code Fellows co-workers – I had a lot of fun working with you. I also would like to personally thank the people who took the time to get to know me and help me see things differently: Dave, Jeff, Adam, Brook, Mitch, and Branton (albeit he never worked at Code Fellows).

Palindromes, Base 16 & Roman Numerals

Leave a Reply

Your email address will not be published. Required fields are marked *