Challenge - 5 Problems
Palindrome Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Palindrome Check for a Simple String
What is the output of the following Java code that checks if a string is a palindrome?
DSA Java
public class Palindrome { public static boolean isPalindrome(String s) { int left = 0, right = s.length() - 1; while (left < right) { if (s.charAt(left) != s.charAt(right)) { return false; } left++; right--; } return true; } public static void main(String[] args) { System.out.println(isPalindrome("radar")); } }
Attempts:
2 left
💡 Hint
Think about how the characters at the start and end of the string compare.
✗ Incorrect
The string "radar" reads the same forwards and backwards, so the method returns true.
❓ Predict Output
intermediateOutput of Palindrome Check with Mixed Case
What is the output of this Java code checking palindrome for a mixed case string?
DSA Java
public class Palindrome { public static boolean isPalindrome(String s) { int left = 0, right = s.length() - 1; while (left < right) { if (s.charAt(left) != s.charAt(right)) { return false; } left++; right--; } return true; } public static void main(String[] args) { System.out.println(isPalindrome("Racecar")); } }
Attempts:
2 left
💡 Hint
Remember that character comparison is case sensitive.
✗ Incorrect
The method compares characters exactly, so 'R' and 'r' are different, causing false.
🔧 Debug
advancedIdentify the Error in Palindrome Check Code
What error will this Java code produce when checking palindrome?
DSA Java
public class Palindrome { public static boolean isPalindrome(String s) { int left = 0, right = s.length(); while (left < right) { if (s.charAt(left) != s.charAt(right)) { return false; } left++; right--; } return true; } public static void main(String[] args) { System.out.println(isPalindrome("level")); } }
Attempts:
2 left
💡 Hint
Check how the right index is initialized and used.
✗ Incorrect
The right index is set to s.length(), which is out of bounds for charAt, causing an exception.
🧠 Conceptual
advancedUnderstanding Palindrome Detection with Ignored Characters
Which approach correctly detects palindromes ignoring spaces and punctuation in Java?
Attempts:
2 left
💡 Hint
Think about how spaces and punctuation affect palindrome checks.
✗ Incorrect
Ignoring non-alphanumeric characters and case ensures correct palindrome detection for phrases.
🚀 Application
expertOutput of Palindrome Check for Linked List
Given a singly linked list representing characters, what is the output after checking if it forms a palindrome?
DSA Java
class Node { char val; Node next; Node(char val) { this.val = val; this.next = null; } } public class PalindromeLinkedList { public static boolean isPalindrome(Node head) { if (head == null) return true; Node slow = head, fast = head; java.util.Stack<Character> stack = new java.util.Stack<>(); while (fast != null && fast.next != null) { stack.push(slow.val); slow = slow.next; fast = fast.next.next; } if (fast != null) slow = slow.next; while (slow != null) { if (stack.pop() != slow.val) return false; slow = slow.next; } return true; } public static void main(String[] args) { Node head = new Node('a'); head.next = new Node('b'); head.next.next = new Node('b'); head.next.next.next = new Node('a'); System.out.println(isPalindrome(head)); } }
Attempts:
2 left
💡 Hint
Think about how the stack helps compare the first half with the second half.
✗ Incorrect
The linked list 'a'->'b'->'b'->'a' reads the same forwards and backwards, so returns true.
