LeetCode: Palindrome Number

Melissa Guachun
3 min readDec 1, 2021

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:

var isPalindrome = function(x) {}

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:

var isPalindrome = function(x) {
let reversed = x.toString().split('').reverse().join('')
}

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.

var isPalindrome = function(x) {
let reversed = x.toString().split('').reverse().join('')
console.log(typeof(reversed))
}

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).

var isPalindrome = function(x) {
let reversed = x.toString().split('').reverse().join('')
return (x.toString() === reversed)
};

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:

var isPalindrome = function(x) {}

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

var isPalindrome = function(x) {
let reversedStr = ''
let xStr = x.toString() // example: xStr = '123'
reversedStr = i + reversedStr
}

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 :

'' + 1
//will return '1'

the second pass, we take:

'2' + '1'//will return '21'
//Remember: we are not doing regular addition because we are dealing with strings

the third pass:

'3' + '21' //which will return '321'

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

1
21
321

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).

var isPalindrome = function(x) {
let reversedStr = '' //empty string
let xStr = x.toString() // example: xStr = '123'
for (let i of xStr() {
reversedStr = i + reversedStr
}
return (reversedStr === xStr)
};

Links:

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

https://www.youtube.com/watch?v=GILypEIcMQM

--

--

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.