LeetCode: Palindrome Number

A walkthrough and explanation of a classic LeetCode question

I , like many others are starting their LeetCode studies to better themselves as engineers and ultimately ace the dreaded technical assessments. Immediately I found myself at odds with the easiest LeetCode questions and felt Imposter Syndrome coming for me within seconds. To avoid letting my fears get the best of me I decided to use this opportunity and turn it into a learning experience to help myself and others better understand the logic behind LeetCode questions.

The Palindrome Number question seems to be a classic which is why I chose to tackle it at first. The task is straight forward, write a function that will determine if an integer is a palindrome and return the outcome. I chose JavaScript as my language of choice but didn’t have much use of it outside the curriculum of my coding bootcamp. As much as I would love to say I spent hours on this question crafting a possible function, I would be lying. I probably looked at the question for about 5 minutes with means of getting close to an answer before I checked the solution. It seems there are two ways to go about solving this problem.

We are given the following to start with:

At first thought, lets think of what a Palindrome integer is again. It is an in integer that can read the same forwards and backwards. So if we can somehow reverse the integer and compare it to the palindrome integer, we can then return the output.

Version 1:

We call a variable reversed and make it equal to the string version of (x) which is x.toString(). To reverse it we have to utilize split('') to divide a string into an ordered list of substrings. Then we reverse the array in place using reverse(). And finally we use join('') which creates and returns a new string by concatenating all of the elements in an array. In this case, the ('') would join the reversed integers with no spaces between each integer in the string.

To test our code we code, we console.log our reversed variable to see what datatype our palindrome integer turned into.

In the console we should see that we changed our integer 121 to a string ‘121’ which is reversed. Our next step is to compare the reversed string of ‘121’ to (x).

Above, we use the return function and compare string version of (x) with the reversed string. If they match it will return a boolean of true or false.

Version 2:

Lets declare an empty string called reversedStr and make xStr equal to x.toStr.

Then we make a for loop to loop through the string version of our example of ‘123’. On the first pass we are looping through i which is :

the second pass, we take:

the third pass:

So if we console.log reverseStr we should see the following:

The output should be the reversed version of our palindrome integer ‘123’. Now we must compare the reversed string with the string version of (x).


I used SantaMaria’s Code to understand this problem. Their video was very helpful!




Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Melissa Guachun

Software Developer and visual artist based in NYC. Join me on my journey to coding enlightenment or a torrential mental breakdown, whichever comes first.