Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Build confidence with an intuition-first walkthrough focused on array fundamentals.
Given an array of strings words, return the first palindromic string in the array. If there is no such string, return an empty string "".
A string is palindromic if it reads the same forward and backward.
Example 1:
Input: words = ["abc","car","ada","racecar","cool"] Output: "ada" Explanation: The first string that is palindromic is "ada". Note that "racecar" is also palindromic, but it is not the first.
Example 2:
Input: words = ["notapalindrome","racecar"] Output: "racecar" Explanation: The first and only string that is palindromic is "racecar".
Example 3:
Input: words = ["def","ghi"] Output: "" Explanation: There are no palindromic strings, so the empty string is returned.
Constraints:
1 <= words.length <= 1001 <= words[i].length <= 100words[i] consists only of lowercase English letters.Problem summary: Given an array of strings words, return the first palindromic string in the array. If there is no such string, return an empty string "". A string is palindromic if it reads the same forward and backward.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Two Pointers
["abc","car","ada","racecar","cool"]
["notapalindrome","racecar"]
["def","ghi"]
valid-palindrome)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2108: Find First Palindromic String in the Array
class Solution {
public String firstPalindrome(String[] words) {
for (var w : words) {
boolean ok = true;
for (int i = 0, j = w.length() - 1; i < j && ok; ++i, --j) {
if (w.charAt(i) != w.charAt(j)) {
ok = false;
}
}
if (ok) {
return w;
}
}
return "";
}
}
// Accepted solution for LeetCode #2108: Find First Palindromic String in the Array
func firstPalindrome(words []string) string {
for _, w := range words {
ok := true
for i, j := 0, len(w)-1; i < j && ok; i, j = i+1, j-1 {
if w[i] != w[j] {
ok = false
}
}
if ok {
return w
}
}
return ""
}
# Accepted solution for LeetCode #2108: Find First Palindromic String in the Array
class Solution:
def firstPalindrome(self, words: List[str]) -> str:
return next((w for w in words if w == w[::-1]), "")
// Accepted solution for LeetCode #2108: Find First Palindromic String in the Array
impl Solution {
pub fn first_palindrome(words: Vec<String>) -> String {
for w in words {
if w == w.chars().rev().collect::<String>() {
return w;
}
}
String::new()
}
}
// Accepted solution for LeetCode #2108: Find First Palindromic String in the Array
function firstPalindrome(words: string[]): string {
return words.find(w => w === w.split('').reverse().join('')) || '';
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair of elements. The outer loop picks one element, the inner loop scans the rest. For n elements that is n × (n−1)/2 comparisons = O(n²). No extra memory — just two loop variables.
Each pointer traverses the array at most once. With two pointers moving inward (or both moving right), the total number of steps is bounded by n. Each comparison is O(1), giving O(n) overall. No auxiliary data structures are needed — just two index variables.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Advancing both pointers shrinks the search space too aggressively and skips candidates.
Usually fails on: A valid pair can be skipped when only one side should move.
Fix: Move exactly one pointer per decision branch based on invariant.