1Check if a Number is a Palindrome
EasyProblem Statement
Given an integer, determine if it is a palindrome. A palindrome number reads the same backward as forward.
Example 1:
Input: 121
Output: TrueExample 2:

1. Write a program to check if a number is a palindrome. 2. Write a program to find the second largest number in an array. 3. Write a program to reverse a string.
Given an integer, determine if it is a palindrome. A palindrome number reads the same backward as forward.
Example 1:
Input: 121
Output: TrueExample 2:
Input: -121
Output: FalseExample 3:
Input: 10
Output: FalseConstraints:
Convert the number to a string and check if it reads the same forwards and backwards.
Steps:
Time Complexity: O(n) - where n is the number of digits
Space Complexity: O(n) - for the string
def isPalindrome(x):
if x < 0:
return False
str_x = str(x)
return str_x == str_x[::-1]
print(isPalindrome(121))
print(isPalindrome(-121))
print(isPalindrome(10))
print(isPalindrome(0))def isPalindrome(x):
if x < 0:
return False
original = x
reversed_num = 0
while x > 0:
Take the number 121. Extract the last digit (1), then the next (2), then the last (1). Build a new number from these digits in reverse order: 1 → 12 → 121. If the reversed number equals the original, it's a palindrome.
You are given a string s.
Your task is to reverse the string without using built‑in reverse functions.
Return the reversed string.
Example 1
Input: s = "hello"
Output: "olleh"
Example 2
Input: s = "TCSNQT"
Output: "TQNSCT"
public class Solution {
public String reverse(String s) {
char[] arr = s.toCharArray();
int left = 0;
int right = arr.length - 1;
while (left < right) {
char temp = arr[left];
arr[left] = arr[right];
arr[right] = temp;
left++;
right--;
}
return new String(arr);
}
public static void main(String[] args) {
Solution sol = new Solution();
System.out.println(sol.reverse("hello")); // olleh
System.out.println(sol.reverse("TCSNQT")); // TQNSCT
}
}You are given two integers a and b.
Your task is to compute:
Return both values.
Example 1
Input: a = 12, b = 18
Output: GCD = 6, LCM = 36
Explanation:
Example 2
Input: a = 7, b = 5
Output: GCD = 1, LCM = 35
public class Solution {
public static int gcd(int a, int b) {
while (b != 0) {
int temp = b;
b = a % b;
a = temp;
}
return a;
}
public static int lcm(int a, int b) {
return (a * b) / gcd(a, b);
}
public static void main(String[] args) {
int a = 12, b = 18;
System.out.println("GCD: " + gcd(a, b)); // 6
System.out.println("LCM: " + lcm(a, b)); // 36
a = 7; b = 5;
System.out.println("GCD: " + gcd(a, b)); // 1
System.out.println("LCM: " + lcm(a, b)); // 35
}
}You are given a string s consisting of lowercase and uppercase English letters.
Your task is to count the number of vowels and consonants in the string.
Return both counts.
Example 1
Input: s = "hello"
Output: Vowels = 2, Consonants = 3
Explanation:
Example 2
Input: s = "TCSNQT"
Output: Vowels = 1, Consonants = 5
Explanation:
public class Solution {
public static void countVowelsAndConsonants(String s) {
int vowels = 0, consonants = 0;
s = s.toLowerCase();
for (char c :
Given an array of integers, find the second largest number in the array.
Example 1:
Input: [10, 5, 20, 8, 15]
Output: 15Example 2:
Input: [10, 10, 10]
Output: None (or -1)Example 3:
Input: [5, 5, 10, 10, 8]
Output: 8Constraints:
Traverse the array once and keep track of the largest and second largest numbers.
Steps:
Time Complexity: O(n) - single pass through array
Space Complexity: O(1) - only use two variables
def secondLargest(arr):
if len(
Given a number N, print the Fibonacci series up to N terms. The Fibonacci series is a sequence where each number is the sum of the two preceding ones, starting from 0 and 1.
Example 1:
Input: N = 5
Output: 0 1 1 2 3Example 2:
Input: N = 8
Output: 0 1 1 2 3 5 8 13Example 3:
Input: N = 1
Output: 0Constraints:
Use two variables to keep track of the previous two numbers and generate the next number in the series.
Steps:
Time Complexity: O(n) - generate n terms
Space Complexity: O(1) - only use two variables
def fibonacci(n):
if n <= 0:
return
Given an integer, determine if it is a prime number. A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself.
Example 1:
Input: 7
Output: TrueExample 2:
Input: 10
Output: FalseExample 3:
Input: 2
Output: TrueConstraints:
Check divisibility only up to the square root, and optimize by handling 2 and 3 separately, then checking only numbers of form 6k±1.
Steps:
Time Complexity: O(√n)
Space Complexity: O(1) - only use variables
def isPrime(n):
if n <= 1:
return
Given a string, reverse it and return the reversed string.
Example 1:
Input: "hello"
Output: "olleh"Example 2:
Input: "Python"
Output: "nohtyP"Example 3:
Input: "a"
Output: "a"Constraints:
Iterate through the string from end to start and build the reversed string.
Steps:
Time Complexity: O(n) - iterate through string once
Space Complexity: O(n) - store reversed string
def reverseString(s):
result = ""
i = len(s) -
Given an array and an integer K, rotate the array to the right by K positions.
Example 1"
Input: arr = [1, 2, 3, 4, 5], K = 2
Output: [4, 5, 1, 2, 3]Example 2:
Input: arr = [10, 20, 30, 40], K = 1
Output: [40, 10, 20, 30]Example 3:
Input: arr = [1, 2, 3], K = 5
Output: [2, 3, 1]Constraints:
Use array slicing to split the array at the rotation point and recombine.
Steps:
Time Complexity: O(n) - create new array with all elements
Space Complexity: O(n) - store rotated array
def rotateArray(arr, k):
if len(arr) ==
Given an array containing n distinct numbers taken from 1 to n+1, find the one number that is missing from the array.
Example 1:
Input: [1, 2, 4, 5, 6]
Output: 3Example 2:
Input: [2, 3, 4, 5, 6, 7, 8]
Output: 1Example 3:
Input: [1, 2, 3, 4, 5, 7, 8]
Output: 6Constraints:
Calculate the expected sum of numbers from 1 to n+1, then subtract the actual sum of array elements.
Steps:
Time Complexity: O(n) - iterate through array once
Space Complexity: O(1) - only use variables
def findMissingNumber(arr):
n = len(arr)
expected_sum =
Imagine watching a race. You keep track of the first place runner and second place runner. Whenever someone faster than first place appears, the first place runner becomes second place, and the new person becomes first. If someone is only faster than second place, they become the new second place. By the end, you know who finished second.
Think of it like climbing stairs where each step height is the sum of the previous two step heights. Start with steps of height 0 and 1. The next step is 0+1=1, then 1+1=2, then 1+2=3, and so on. You keep adding the last two numbers to get the next one in the sequence.
All primes greater than 3 are of the form 6k±1. Why? Because numbers can be written as 6k, 6k+1, 6k+2, 6k+3, 6k+4, or 6k+5. But 6k, 6k+2, and 6k+4 are divisible by 2, and 6k+3 is divisible by 3. So we only need to check 6k+1 and 6k+5 (which is 6k-1). This reduces checks by about 66% compared to checking all odd numbers.
Imagine reading a book backwards - you start from the last page and work your way to the first. Here we start from the last character of the string and add each character to a new string until we reach the beginning. The result is the original string in reverse order.
Imagine a circular conveyor belt with items on it. Rotating by K positions means taking the last K items and moving them to the front. We split the array at position (n-K), so the last K elements become the first part of our new array, and the remaining elements follow. Using modulo handles cases where K is larger than the array length, as rotating by array length brings us back to the start.
Imagine you have a piggy bank that should contain $55 (sum of 1+2+3+...+10). You count the money and find only $52. The missing amount is $3. Similarly, we calculate what the sum should be if all numbers were present, then subtract what we actually have. The difference tells us which number is missing.