Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction 
2.
Problem Statement
3.
Solution
4.
Frequently Asked Questions
4.1.
Can a negative number be a palindrome?
4.2.
What is a palindrome number? 
4.3.
What is an anagram give an example of an anagram?
5.
Conclusion
Last Updated: Mar 27, 2024

Last Palindromic Date

Interview Puzzles

Introduction 

As we all know, puzzles can have a significant impact on our development. Puzzles help us develop our problem-solving and critical-thinking abilities, which are both necessary for mastery of other skills later in life. So let us now discuss the puzzle last palindromic date.

This article will discuss a famous interview puzzle named the last palindromic date. But before discussing the puzzle, we should first discuss what a palindrome is. A palindrome is a word that reads the same from starting and ending.

For example, consider this word tenet,

Illustration Image

It reads the same from forward and backward.

Now, since we know what a palindrome is, we should look at the problem statement of the puzzle.

Problem Statement

We are given a date of the year 2001; the date is October 2, 2001. If we convert this date to MM/DD/YYYY format, this date can be written as 10/02/2001. This date can be considered a palindrome since it reads the same from forward and backward. Since we can consider this date a palindrome, therefore, we can call this date a palindromic date. Our task is to find a palindromic date that occurred just before this date. Examples: 11/02/2011 if we put the date in a string as 11022011 and reverse it, it would give us 11022011 which is the same as the original string thus making it and the date a palindrome. Similarly, if we put the date 12/02/2021 into a string as 12022021 and reverse it, it would give us 12022021 which is the same as the original string making it a palindrome.

Solution

We can conclude that one year can have only one palindromic date if we observe. In 2001 there is only one palindromic date, i.e., 10/02/2001. Since the date fixes month and day, we can't have more than one palindromic date in a year. 

Since we require a valid palindromic date before the given date, we will first check the year 1900. If we find any valid palindromic date, that would be our answer. Else, we will decrease the year. If we check the year 1900, then the date would be 91, which is not possible; therefore, we will decrease the year's value. We will check for 1800, which will have the date 81, which again is not possible, so after decreasing the years from 1900 to 1400, we will arrive at 1300, where the date would be 31.   

So now, our task would be to check the latest year and month that would make palindrome with the date 31. So if we consider the year 1390, then the date becomes 09/31/1390 but is this a valid date?. The answer is no because September has 30 days, so now we will check the year 1380. Here the date becomes 08/31/1380. Is this a valid date?. The answer is yes because August has 31 days.

But wait, there is one question, i.e., Why didn't we consider years b/w 1390 and 1400? This answer is simple; if we consider these years, then months will exceed 12. For example, consider the year 1391 here. The date would be 19/31/1391. Here the months exceed 12, so this date becomes invalid.

You can also read about Palindrome Number in Python here.

Frequently Asked Questions

Can a negative number be a palindrome?

An integer x is a palindrome if reverse(x) = x, where reverse(x) is x with its digits reversed. So, negative numbers are not palindromic.

What is a palindrome number? 

Numbers that read the same backward and forwards are called palindrome numbers. The numbers 17371, 3, and 121 are palindrome numbers.

What is an anagram give an example of an anagram?

An anagram of a string is another string that has the same no of characters in it but the order of characters is changed.
For example, 
abba is an anagram of baba

Conclusion

This article briefly discussed the puzzle to find the last palindromic date. I hope that you must have gained some knowledge about palindromes after reading this blog.

Recommended Readings:


Do check out The Interview guide for Product Based Companies as well as some of the Popular Interview Problems from Top companies like Amazon, Adobe, Google, etc. on Coding Ninjas Studio.

Check out this problem - Check If A String Is Palindrome

Also check out some of the Guided Paths on topics such as Interview Puzzles, Data Structure and Algorithms, Competitive Programming, Operating Systems, Computer Networks, DBMS, System Design, etc. as well as some Contests, Test Series, Interview Bundles, and some Interview Experiences curated by top Industry Experts only on Coding Ninjas Studio.

Happy Learning!

Live masterclass