[Go to site: main page, start]

0% found this document useful (0 votes)
3 views1 page

Java String DSA Interview Questions

The document contains Java programming interview questions and answers related to string manipulation. It includes examples for reversing a string, checking for palindromes, determining if two strings are anagrams, counting vowels and consonants, and finding the length of the longest substring without repeating characters. Each example provides the code and the expected output.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views1 page

Java String DSA Interview Questions

The document contains Java programming interview questions and answers related to string manipulation. It includes examples for reversing a string, checking for palindromes, determining if two strings are anagrams, counting vowels and consonants, and finding the length of the longest substring without repeating characters. Each example provides the code and the expected output.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Java String DSA Interview Questions and Answers 1■■ Reverse a String Question: Write a Java

program to reverse a given string.


public class ReverseString { public static void main(String[] args) { String str = "hello"; String
reversed = ""; for (int i = [Link]() - 1; i >= 0; i--) { reversed += [Link](i); }
[Link]("Reversed String: " + reversed); } } Output: Reversed String: olleh 2■■ Check
for Palindrome Question: Check if the given string is a palindrome or not.
public class PalindromeString { public static void main(String[] args) { String str = "madam"; String
rev = new StringBuilder(str).reverse().toString(); if ([Link](rev))
[Link]("Palindrome"); else [Link]("Not Palindrome"); } } Output:
Palindrome 3■■ Check if Two Strings are Anagrams Question: Determine if two strings are
anagrams.
import [Link]; public class AnagramCheck { public static void main(String[] args) { String s1
= "listen"; String s2 = "silent"; char[] a = [Link](); char[] b = [Link]();
[Link](a); [Link](b); if ([Link](a, b)) [Link]("Anagram"); else
[Link]("Not Anagram"); } } Output: Anagram 4■■ Count Vowels and Consonants
Question: Count the number of vowels and consonants in a string.
public class CountVowelsConsonants { public static void main(String[] args) { String str =
"welcome"; str = [Link](); int vowels = 0, consonants = 0; for (int i = 0; i < [Link](); i++)
{ char ch = [Link](i); if ("aeiou".indexOf(ch) != -1) vowels++; else if (ch >= 'a' && ch <= 'z')
consonants++; } [Link]("Vowels: " + vowels); [Link]("Consonants: " +
consonants); } } Output: Vowels: 3
Consonants: 4 5■■ Longest Substring Without Repeating Characters Question: Find the length of
the longest substring without repeating characters.
import [Link]; public class LongestUniqueSubstring { public static void main(String[]
args) { String s = "abcabcbb"; int maxLength = 0; for (int i = 0; i < [Link](); i++) { HashSet set =
new HashSet<>(); for (int j = i; j < [Link](); j++) { if ([Link]([Link](j))) break;
[Link]([Link](j)); maxLength = [Link](maxLength, j - i + 1); } } [Link]("Length of
Longest Unique Substring: " + maxLength); } } Output: Length of Longest Unique Substring: 3

You might also like