Prepin
Log in

Identify the palindromes.

Approach / Explanation

Your task in this question is to devise an algorithm that can determine whether a given input string is a palindrome. A palindrome is defined as a string that reads the same forwards and backwards, such as "madam" or "racecar." To tackle this problem, you can employ various algorithms, commonly involving the comparison of characters at mirrored positions, typically beginning with the first and last characters of the string, followed by the second and second-to-last, and so on. Here are some important aspects to consider: How to address spaces, punctuation, and case sensitivity. If the question does not specify these details, you may need to seek clarification. How to treat empty strings. Whether the algorithm is permitted to utilize built-in functions or must be created from the ground up.

Suggested Answer

A straightforward method to determine if a string is a palindrome is to reverse it and compare the reversed version with the original. If they match, the string is a palindrome. Here’s a simple example using Python: def is_palindrome ( str ) : reverse_str = str [ : : - 1 ] if ( str == reverse_str ) : return True else : return False This function reverses the string and checks for equality with the original. It returns True if they are the same, and False otherwise.

Alternative Answer

Another approach involves using a two-pointer technique. In this method, we will set one pointer at the beginning of the string and the other at the end. We will compare the characters at these two pointers, and if they match, we will move both pointers inward towards the center of the string until they converge. Here’s an example in JavaScript: function is_palindrome ( str ) { let start = 0 ; let end = str . length - 1 ; while ( start < end ) { if ( str [ start ] !== str [ end ] ) { return false ; } start ++ ; end -- ; } return true ; } This function utilizes the two-pointer technique. It begins by setting two pointers at the beginning and end of the string. Then, within a loop, it verifies if the characters at the pointers are identical. If they are not, it promptly returns False. If they match, it advances the pointers towards the center. If the loop concludes without encountering any discrepancies, it returns True.

Question Details

Difficulty & Category

Medium
algorithms

Companies

Apple

Product

For CandidatesFor RecruitersCompaniesBrowse JobsInterview QuestionsDeep ProfileSkill AssessmentOpportunity Matching
Prepin.ai

© 2026 Prepin LLC · Philadelphia, PA · All rights reserved.