0% found this document useful (0 votes)
2 views4 pages

Java Coding Round Important Programs

The document outlines common coding problems and their solutions in Java, including reversing a string, checking for palindromes, finding the maximum repeated character, removing duplicates from an array, and solving the two-sum problem. Additional problems include finding the second largest number, sorting an array using bubble sort, counting frequency with HashMap, identifying the longest consecutive sequence, generating a Fibonacci series, checking for prime numbers, and verifying anagrams. Each problem is accompanied by a complete Java code implementation.
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)
2 views4 pages

Java Coding Round Important Programs

The document outlines common coding problems and their solutions in Java, including reversing a string, checking for palindromes, finding the maximum repeated character, removing duplicates from an array, and solving the two-sum problem. Additional problems include finding the second largest number, sorting an array using bubble sort, counting frequency with HashMap, identifying the longest consecutive sequence, generating a Fibonacci series, checking for prime numbers, and verifying anagrams. Each problem is accompanied by a complete Java code implementation.
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

Coding Round (Important)

Be ready to write code on whiteboard / system.

Common Programs:

1. Reverse a String

public class Main {


public static void main(String[] args) {

String str = "Java";


String rev = "";

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


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

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


}
}

2. Palindrome Check

public class Main {


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

3. Find Maximum Repeated Character

public class Main {


public static void main(String[] args) {

String str = "programming";


int max = 0;
char result = ' ';

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


int count = 0;
for(int j = 0; j < [Link](); j++) {
if([Link](i) == [Link](j)) {
count++;
}
}
if(count > max) {
max = count;
result = [Link](i);
}
}

[Link]("Max Repeated Character: " + result);


}
}

4. Remove Duplicates from Array


import [Link].*;

public class Main {


public static void main(String[] args) {

int arr[] = {1,2,2,3,4,4,5};

Set<Integer> set = new LinkedHashSet<>();

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


[Link](arr[i]);
}

[Link](set);
}
}

5. Two Sum Problem

public class Main {


public static void main(String[] args) {

int arr[] = {2,7,11,15};


int target = 9;

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


for(int j = i + 1; j < [Link]; j++) {
if(arr[i] + arr[j] == target) {
[Link]("Index: " + i + " , " + j);
}
}
}
}
}

6. Find Second Largest Number

public class Main {


public static void main(String[] args) {

int arr[] = {10, 20, 5, 8, 25};


int largest = Integer.MIN_VALUE;
int second = Integer.MIN_VALUE;

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


if(arr[i] > largest) {
second = largest;
largest = arr[i];
} else if(arr[i] > second && arr[i] != largest) {
second = arr[i];
}
}

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


}
}

7. Sort Array (Bubble Sort)

public class Main {


public static void main(String[] args) {

int arr[] = {5,3,8,4,2};

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


for(int j = 0; j < [Link]-i-1; j++) {
if(arr[j] > arr[j+1]) {
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}

for(int num : arr)


[Link](num + " ");
}
}

8. Count Frequency Using HashMap

import [Link].*;

public class Main {


public static void main(String[] args) {

int arr[] = {1,2,2,3,3,3};

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

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


if([Link](arr[i])) {
[Link](arr[i], [Link](arr[i]) + 1);
} else {
[Link](arr[i], 1);
}
}

[Link](map);
}
}

9. Longest Consecutive Sequence

import [Link].*;

public class Main {


public static void main(String[] args) {

int arr[] = {100,4,200,1,3,2};


Set<Integer> set = new HashSet<>();

for(int num : arr)


[Link](num);

int longest = 0;

for(int num : set) {


if(![Link](num - 1)) {
int current = num;
int count = 1;

while([Link](current + 1)) {
current++;
count++;
}

longest = [Link](longest, count);


}
}

[Link]("Longest Sequence: " + longest);


}
}

10. Fibonacci Series


public class Main {
public static void main(String[] args) {

int n = 10;
int a = 0, b = 1;

[Link](a + " " + b + " ");

for(int i = 2; i < n; i++) {


int c = a + b;
[Link](c + " ");
a = b;
b = c;
}
}
}

11. Prime Number Check

public class Main {


public static void main(String[] args) {

int num = 7;
boolean flag = true;

for(int i = 2; i <= num/2; i++) {


if(num % i == 0) {
flag = false;
break;
}
}

if(flag && num > 1)


[Link]("Prime");
else
[Link]("Not Prime");
}
}

12. Anagram Check

import [Link];

public class Main {


public static void main(String[] args) {

String s1 = "listen";
String s2 = "silent";

char a1[] = [Link]();


char a2[] = [Link]();

[Link](a1);
[Link](a2);

if([Link](a1, a2))
[Link]("Anagram");
else
[Link]("Not Anagram");
}
}

You might also like