Frequently Asked Java Codes in Job Interviews — Question 1

Checking Whether a Given Word is a Palindrome

Amesh Jayaweera
4 min readMay 22, 2020

Hi guys, from this article series I would like to talk about the frequently asked java codes at job interviews. If you are finding a job related to the software engineering field, you will definitely be benefitted from this article. The reason why I included Java in the title is that most of the interview coding questions are asked from Java (from an object-oriented perspective).

In this article, I would like to discuss how to write the code to check whether a given word is a palindrome or not, which is a commonly asked question at interviews.

Photo by Daoud Abismail on Unsplash

Building Up an Algorithm to Recognize a Palindrome

First of all, let’s understand what a palindrome is. If we can read a given word the same backward as forward, then we can say that word is a palindrome. Some examples of palindromes are madam’, racecar’, etc. Now you know what a palindrome is.

Just by knowing what a palindrome is, we can’t start the coding. So now it’s time to build an algorithm for our problem. It is always a good practice to follow this procedure before starting to code, and it will avoid messing things up when you are actually writing the code.

In this problem, there are two instances as follows.

1. The given word is a palindrome word.
2. The given word is not a palindrome word.

So, let’s consider two-sample test cases for those instances.

Test Case 1 — The given word is a palindrome word.

Input string (word) — racecar

We can say that the given word “racecar” is a palindrome word just by looking at it. But when it comes to programming, we have to think of a proper mechanism to check it. We know if a word is a palindrome, that word can be read the same backward as forward. What does it mean?

Figure 1 by Author

Figure 1 shows how the string “racecar” is stored in memory. It gives you a hint to find a mechanism to check whether the given word is palindrome or not. Let’s take a variable called word’, which contains the given input string.
Then we can write our logic as follows,

1 → IF word[0] == word[6] -> ‘r’ == ‘r’ -> True

2 → IF word[1] == word[5] -> ‘a’ == ‘a’ -> True

3 → IF word[2] == word[4] -> ‘c’ == ‘c’ -> True

Likewise, if all the conditions are true, then we can say that the given word is a palindrome.

Let’s discuss the 2nd test case.

Test Case 2 — The given word is not a palindrome word.

Input string (word) — racecrr (notice the difference in spelling)

Just by looking at the input string, we can say that it’s not a palindrome word.

Figure 2 By Author

1 → IF word[0] == word[6] -> ‘r’ == ‘r’ -> True

2 → IF word[1] == word[5] -> ‘a’ == ‘r’ -> False

3 → IF word[2] == word[4] -> ‘c’ == ‘c’ -> True

There are 3 conditions above. You can see that the 2nd condition returns false. Because the 2nd character of the string is not matched to the 6th character. So no need to check the remaining characters of the string. Because if one condition returns false, at that point we can decide that the given word is not a palindrome.

We discussed our two test cases. Now we have the confidence to build our algorithm. Let’s do it.

For that, we should consider a general case.
Suppose that the input string is assigned to the variable called word and the length of the input string is n. Then we can write a general condition as follows,

IF word[index] == word [n-(index+1)] // Note that index is starting at Zero

In test case 2, I mentioned that if one condition returns false, at that point we can decide the given word is not a palindrome. So we can modify the above condition as follows.

IF word[index] != word [n-(index+1)]

This condition returns true if the word is not a palindrome. And at that point, we can terminate our program by returning something. If the above condition never returns true, then the input string is a palindrome word. So the algorithm can be written as follows.

Algorithm isPalindrome(word : String) :

FOR index -> 0 to (n/2) :

IF word[index] != word[n — (index+1)] :

RETURN “(word) is not a palindrome word.”
RETURN “(word) is a palindrome word.”

Now we have an algorithm and we can convert it into a Java code. The Java code for the above can be written as follows.

Screenshot by Author

Hope to see you soon with more articles in the future.

Thank you for reading.

--

--

Amesh Jayaweera

I’m passionate to explore the knowledge and improve my skills level best in Science & Technology including Programming, and Artificial Intelligence.