[Go to site: main page, start]

0% found this document useful (0 votes)
3 views12 pages

Java Interview Questions

This document is a quick-reference guide for Java interview questions focused on strings and arrays. It includes explanations, code snippets, and logic for various common problems such as reversing a string, checking for palindromes, removing duplicates, and finding missing numbers. Each section provides a Java code example, logic breakdown, and time complexity analysis.

Uploaded by

shubhu9211
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 views12 pages

Java Interview Questions

This document is a quick-reference guide for Java interview questions focused on strings and arrays. It includes explanations, code snippets, and logic for various common problems such as reversing a string, checking for palindromes, removing duplicates, and finding missing numbers. Each section provides a Java code example, logic breakdown, and time complexity analysis.

Uploaded by

shubhu9211
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 Interview Questions

Strings & Arrays — Explanations, Code, Logic & Output

A Quick-Reference Practice Guide


Table of Contents

1. Reverse a String

2. Check Whether a String is Palindrome

3. Remove Duplicate Characters

4. Count Frequency of Each Character

5. Find First Non-Repeated Character

6. Check Whether Two Strings are Anagrams

7. Reverse Each Word in a Sentence

8. Find Duplicate Characters in a String

9. Find Missing Number from a Sorted Array

10. Find the Second Largest Element in an Array


1. Reverse a String

Reverse the characters of a given string without changing the original string. This is one of the most frequently asked beginner
Java interview questions.

Java Code

public class ReverseString {

public static void main(String[] args) {

String str = "Infosys";


String rev = "";

for(int i = [Link]()-1; i >= 0; i--) {


rev = rev + [Link](i);
}

[Link]("Reversed String = " + rev);


}
}

Logic

1. Store the string in str.


2. Start from the last character.
3. Append each character to rev.
4. Continue until index becomes 0.
5. Print the reversed string.

Output
Reversed String = sysofnI

Time Complexity: O(n)


2. Check Whether a String is Palindrome

A palindrome reads the same forward and backward.

madam ✔
level ✔
java ✘

Java Code

public class Palindrome {

public static void main(String[] args) {

String str = "madam";


String rev = "";

for(int i = [Link]()-1; i >= 0; i--) {


rev += [Link](i);
}

if([Link](rev))
[Link]("Palindrome");
else
[Link]("Not Palindrome");
}
}

Logic

1. Reverse the string.


2. Compare original and reversed strings using equals().
3. If equal → Palindrome.
4. Else → Not palindrome.

Time Complexity: O(n)


3. Remove Duplicate Characters

Remove repeated characters while preserving the order.

Example

Programming
Output: Progamin

Java Code

import [Link];

public class RemoveDuplicates {

public static void main(String[] args) {

String str = "Programming";

LinkedHashSet<Character> set = new LinkedHashSet<>();

for(char ch : [Link]()) {
[Link](ch);
}

for(char ch : set) {
[Link](ch);
}
}
}

Logic

1. Convert string into characters.


2. Store characters inside LinkedHashSet.
3. Set automatically removes duplicates.
4. LinkedHashSet preserves insertion order.
5. Print all elements.

Output

Progamin

Time Complexity: O(n)


4. Count Frequency of Each Character

Find how many times every character appears in a string.

Example

apple
Output:
a = 1
p = 2
l = 1
e = 1

Java Code

import [Link];

public class Frequency {

public static void main(String[] args) {

String str = "apple";

HashMap<Character,Integer> map = new HashMap<>();

for(char ch : [Link]()) {

if([Link](ch))
[Link](ch, [Link](ch)+1);
else
[Link](ch,1);
}

[Link](map);
}
}

Logic

1. Read one character.


2. If character already exists in map, increase count.
3. Otherwise insert with count = 1.
4. Continue until string ends.

Output

{a=1, p=2, l=1, e=1}

Time Complexity: O(n)


5. Find First Non-Repeated Character

Find the first character that appears exactly once.

Example

stress
Output: t

Java Code

import [Link];

public class FirstNonRepeated {

public static void main(String[] args) {

String str = "stress";

LinkedHashMap<Character,Integer> map = new LinkedHashMap<>();

for(char ch : [Link]()) {
[Link](ch, [Link](ch,0)+1);
}

for(char ch : [Link]()) {

if([Link](ch)==1) {
[Link](ch);
break;
}
}
}
}

Logic

1. Store each character and its frequency in a LinkedHashMap.


2. LinkedHashMap maintains insertion order.
3. Traverse the map.
4. The first character with frequency 1 is the answer.
5. Break after finding it.
6. Check Whether Two Strings are Anagrams

Two strings are anagrams if they contain the same characters with the same frequency, but the order can be different.

listen → silent ✔
heart → earth ✔
java → lava ✘

Java Code

import [Link];

public class Anagram {


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");
}
}

Logic

1. Convert both strings into character arrays.


2. Sort both arrays.
3. Compare the sorted arrays.
4. If both are equal, they are anagrams.

Time Complexity: O(n log n)


7. Reverse Each Word in a Sentence

Reverse every word individually while keeping the word order the same.

Example

Java Selenium
Output: avaJ muineleS

Java Code

public class ReverseWords {

public static void main(String[] args) {

String str = "Java Selenium";

String words[] = [Link](" ");

for(String word : words){

for(int i = [Link]()-1; i>=0; i--){


[Link]([Link](i));
}

[Link](" ");
}
}
}

Logic

1. Split the sentence using space.


2. Reverse each word individually.
3. Print a space after each reversed word.

Time Complexity: O(n)


8. Find Duplicate Characters in a String

Print all characters that appear more than once.

Example

Programming
Output:
r
g
m

Java Code

import [Link];

public class DuplicateCharacters {

public static void main(String[] args) {

String str = "Programming";

HashMap<Character,Integer> map = new HashMap<>();

for(char ch : [Link]()){
[Link](ch,[Link](ch,0)+1);
}

for(char ch : [Link]()){

if([Link](ch)>1)
[Link](ch);
}
}
}

Logic

1. Count the frequency of every character.


2. Traverse the map.
3. Print characters having frequency greater than 1.

Time Complexity: O(n)


9. Find Missing Number from a Sorted Array

Find the missing number when only one number is missing from a sorted sequence.

Example

1 2 3 4 6 7 8
Output: 5

Java Code

public class MissingNumber {

public static void main(String[] args) {

int arr[] = {1,2,3,4,6,7,8};

for(int i=0;i<[Link]-1;i++){

if(arr[i+1] != arr[i]+1){
[Link]("Missing Number = "+(arr[i]+1));
}
}
}
}

Logic

1. Compare every element with the next element.


2. If the difference is not 1, the missing number is current + 1.
3. Print the missing number.

Time Complexity: O(n)


10. Find the Second Largest Element in an Array

Find the second highest number without sorting the array.

Example

10 50 20 80 70
Output: 70

Java Code

public class SecondLargest {

public static void main(String[] args) {

int arr[] = {10,50,20,80,70};

int largest = Integer.MIN_VALUE;


int second = Integer.MIN_VALUE;

for(int num : arr){

if(num > largest){


second = largest;
largest = num;

}else if(num > second && num != largest){


second = num;
}
}

[Link]("Second Largest = " + second);


}
}

Logic

1. Initialize largest and second to the smallest integer value.


2. If the current number is greater than largest, update both largest and second.
3. Otherwise, if it's greater than second but not equal to largest, update second.
4. Print the second largest element.

End of Guide

You might also like