For better or worse, this question seems to come up all the time for software engineering roles. You talk to a recruiter and get screened a little on background and desired salary. Next up, you talk to someone over the phone and then you screen share and do some bullshit "programming exercise", like write code that can detect if it received a palindrome.
I joked with a friend of mine that I was 3-4 on the palindrome question. What I didn't understand was why did the one company pass on me when the other three proceeded to move me to the next round?
"Did you use recurssion?" he asked.
"Um, no. I just take a string and then create a new string by iterating over the first string backwards. Then I compare the two strings with the equals method," I was confused.
"Yeah, you should create a function that trims the first character of the string and compares it to the last character. If equal, send a trimmed string back to the function and repeat. It's faster this way because it breaks the loop at the first sign of not being a match."
So, here it is. A recursive palindrome tester in Java. Clone this repo.
javac Palindrome.java
java Palindrome Taco cat
Comes back true.
java Palindrome Coding interviews are stupid
Comes back false, but the real answer is true.
For all of this supposed shortage of talent in tech, it is hiring practices like this that are causing the problem. As an employer in an industry that values INNOVATION why interview candidates based on their ability do rote memorization? Do you want to hire canidates who read "Cracking the Coding Interview" or candidates who can actually think?
Most problems in Software Engineering come from being able to understand requirements, push back on bad decisions, and knowing how to do CRUD operations. Knowing how to store the data, secure the data, and retrieve the data along with sensible validation rules are waaaaaaaaay more important than memorizing someone else's algorithm and hoping to win the algorithm lottery.
So next time you're at an interview see if you can showcase some of your code on github, or talk about how you solved a problem in a previous role. If they still want to test a string to see if it's a palindrome, ask yourself, "Do I really want to work here?" If the answer is still yes, then pretend this is the very first time you heard this question and regurgitate this answer. You are welcome.
Personally, I think I would do my best Nancy Reagan impression and "Just say no!" Unless, of course I was interviewing at one of the FANG companies that everyone on the Blind insists is offering an $80k signing bonus, $200k in salary, and $300k in Restricted Stock Units, in which case:
I'm your private dancer, A dancer for money, And any old music will do โ Tina Turner