Skip to content

Commit ac200af

Browse files
feat:Java Interview Coding Examples.
1 parent 964c946 commit ac200af

30 files changed

+604
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package array.manipulations;
2+
3+
public class BubbleSortExample {
4+
public static void main(String[] args) {
5+
int[] arr = {5, 3, 8, 1, 2};
6+
7+
for (int i = 0; i < arr.length - 1; i++) {
8+
for (int j = 0; j < arr.length - i - 1; j++) {
9+
if (arr[j] > arr[j + 1]) {
10+
// Swap
11+
int temp = arr[j];
12+
arr[j] = arr[j + 1];
13+
arr[j + 1] = temp;
14+
}
15+
}
16+
}
17+
18+
System.out.println("Sorted array: ");
19+
for (int num : arr) {
20+
System.out.print(num + " ");
21+
}
22+
}
23+
}
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package array.manipulations;
2+
3+
import java.util.*;
4+
5+
public class FindDuplicate {
6+
public static void main(String[] args) {
7+
int[] arr = {1, 3, 4, 2, 2};
8+
9+
Set<Integer> seen = new HashSet<>();
10+
for (int num : arr) {
11+
if (!seen.add(num)) {
12+
System.out.println("Duplicate number: " + num);
13+
return;
14+
}
15+
}
16+
17+
System.out.println("No duplicates found.");
18+
}
19+
}
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package array.manipulations;
2+
3+
import java.util.*;
4+
5+
public class FindPairs {
6+
public static void main(String[] args) {
7+
int[] arr = {2, 4, 3, 5, 7, 8, 9};
8+
int target = 10;
9+
10+
Set<Integer> seen = new HashSet<>();
11+
12+
for (int num : arr) {
13+
int complement = target - num;
14+
if (seen.contains(complement)) {
15+
System.out.println("Pair: (" + complement + ", " + num + ")");
16+
}
17+
seen.add(num);
18+
}
19+
}
20+
}
21+
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package array.manipulations;
2+
3+
public class MinMaxInArray {
4+
public static void main(String[] args) {
5+
int[] arr = {45, 3, 99, 12, 0, -5, 72};
6+
7+
int min = arr[0];
8+
int max = arr[0];
9+
10+
for (int num : arr) {
11+
if (num < min) min = num;
12+
if (num > max) max = num;
13+
}
14+
15+
System.out.println("Min: " + min);
16+
System.out.println("Max: " + max);
17+
}
18+
}
19+
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package array.manipulations;
2+
3+
public class MissingNumber {
4+
public static void main(String[] args) {
5+
int[] arr = {1, 2, 4, 5, 6}; // Missing 3
6+
7+
int n = arr.length + 1;
8+
int expectedSum = n * (n + 1) / 2;
9+
10+
int actualSum = 0;
11+
for (int num : arr) {
12+
actualSum += num;
13+
}
14+
15+
int missing = expectedSum - actualSum;
16+
System.out.println("Missing number: " + missing);
17+
}
18+
}
19+
+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package array.manipulations;
2+
3+
public class MoveZeros {
4+
public static void main(String[] args) {
5+
int[] arr = {0, 1, 0, 3, 12};
6+
7+
int index = 0;
8+
for (int num : arr) {
9+
if (num != 0) {
10+
arr[index++] = num;
11+
}
12+
}
13+
14+
while (index < arr.length) {
15+
arr[index++] = 0;
16+
}
17+
18+
System.out.print("After moving zeros: ");
19+
for (int num : arr) {
20+
System.out.print(num + " ");
21+
}
22+
}
23+
}
24+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package array.manipulations;
2+
3+
import java.util.*;
4+
5+
public class RemoveDuplicates {
6+
public static void main(String[] args) {
7+
int[] arr = {1, 3, 2, 3, 4, 1, 5};
8+
9+
Set<Integer> set = new LinkedHashSet<>();
10+
for (int value : arr) {
11+
set.add(value);
12+
}
13+
14+
System.out.println("Array after removing duplicates: " + set);
15+
}
16+
}
17+
+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package array.manipulations;
2+
3+
public class ReverseArray {
4+
public static void main(String[] args) {
5+
int[] arr = {1, 2, 3, 4, 5};
6+
7+
int left = 0, right = arr.length - 1;
8+
9+
while (left < right) {
10+
int temp = arr[left];
11+
arr[left] = arr[right];
12+
arr[right] = temp;
13+
14+
left++;
15+
right--;
16+
}
17+
18+
System.out.println("Reversed array: ");
19+
for (int num : arr) {
20+
System.out.print(num + " ");
21+
}
22+
}
23+
}
24+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package stream.manipulations;
2+
3+
import java.util.Arrays;
4+
import java.util.List;
5+
import java.util.stream.Collectors;
6+
7+
public class ArrayToListStream {
8+
public static void main(String[] args) {
9+
String[] names = {"Alice", "Bob", "Charlie"};
10+
11+
List<String> nameList = Arrays.stream(names)
12+
.collect(Collectors.toList());
13+
14+
System.out.println("List: " + nameList);
15+
}
16+
}
17+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package stream.manipulations;
2+
3+
import java.util.Arrays;
4+
5+
public class FilterEvenNumbers {
6+
public static void main(String[] args) {
7+
int[] arr = {1, 2, 3, 4, 5, 6};
8+
9+
int[] evens = Arrays.stream(arr)
10+
.filter(num -> num % 2 == 0)
11+
.toArray();
12+
13+
System.out.println("Even numbers: " + Arrays.toString(evens));
14+
}
15+
}
16+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package stream.manipulations;
2+
3+
import java.util.*;
4+
import java.util.function.Function;
5+
import java.util.stream.Collectors;
6+
7+
public class FrequencyCountStream {
8+
public static void main(String[] args) {
9+
String[] input = {"apple", "banana", "apple", "orange", "banana", "apple"};
10+
11+
Map<String, Long> frequencyMap = Arrays.stream(input)
12+
.collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
13+
14+
System.out.println("Frequencies: " + frequencyMap);
15+
}
16+
}
17+
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package stream.manipulations;
2+
3+
import java.util.OptionalInt;
4+
import java.util.stream.IntStream;
5+
6+
public class MinMaxStream {
7+
public static void main(String[] args) {
8+
int[] arr = {5, 10, 3, 7, 1};
9+
10+
OptionalInt min = IntStream.of(arr).min();
11+
OptionalInt max = IntStream.of(arr).max();
12+
13+
min.ifPresent(m -> System.out.println("Min: " + m));
14+
max.ifPresent(m -> System.out.println("Max: " + m));
15+
}
16+
}
17+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package stream.manipulations;
2+
3+
import java.util.Arrays;
4+
import java.util.stream.IntStream;
5+
6+
public class RemoveDuplicatesStream {
7+
public static void main(String[] args) {
8+
int[] arr = {1, 2, 3, 2, 4, 1, 5};
9+
10+
int[] result = IntStream.of(arr)
11+
.distinct()
12+
.toArray();
13+
14+
System.out.println("Unique elements: " + Arrays.toString(result));
15+
}
16+
}
17+
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package stream.manipulations;
2+
3+
import java.util.Arrays;
4+
5+
public class SortStream {
6+
public static void main(String[] args) {
7+
int[] arr = {9, 3, 7, 1, 4};
8+
9+
int[] sorted = Arrays.stream(arr)
10+
.sorted()
11+
.toArray();
12+
13+
System.out.println("Sorted array: " + Arrays.toString(sorted));
14+
}
15+
}
16+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package stream.manipulations;
2+
3+
import java.util.stream.IntStream;
4+
5+
public class SumWithStreams {
6+
public static void main(String[] args) {
7+
int[] arr = {1, 2, 3, 4, 5};
8+
9+
int sum = IntStream.of(arr).sum();
10+
11+
System.out.println("Sum = " + sum);
12+
}
13+
}
14+
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package string.manipulations;
2+
3+
import java.util.*;
4+
5+
//Example: "listen" & "silent" → true
6+
7+
public class AnagramCheck {
8+
public static boolean areAnagrams(String s1, String s2) {
9+
if (s1.length() != s2.length()) return false;
10+
char[] c1 = s1.toCharArray();
11+
char[] c2 = s2.toCharArray();
12+
Arrays.sort(c1);
13+
Arrays.sort(c2);
14+
return Arrays.equals(c1, c2);
15+
}
16+
17+
public static void main(String[] args) {
18+
System.out.println("Are anagrams: " + areAnagrams("listen", "silent"));
19+
}
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package string.manipulations;
2+
3+
import java.util.*;
4+
5+
// Example: "aabbccdfe" → 'd'
6+
7+
public class FirstNonRepeated {
8+
public static char firstNonRepeatedChar(String str) {
9+
Map<Character, Integer> map = new LinkedHashMap<>();
10+
for (char c : str.toCharArray()) {
11+
map.put(c, map.getOrDefault(c, 0) + 1);
12+
}
13+
for (Map.Entry<Character, Integer> entry : map.entrySet()) {
14+
if (entry.getValue() == 1) return entry.getKey();
15+
}
16+
return '_';
17+
}
18+
19+
public static void main(String[] args) {
20+
System.out.println("First non-repeated character: " + firstNonRepeatedChar("aabbccdfe"));
21+
}
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package string.manipulations;
2+
3+
public class LongestPalindrome {
4+
public static String longestPalindrome(String s) {
5+
int start = 0, end = 0;
6+
for (int i = 0; i < s.length(); i++) {
7+
int len1 = expand(s, i, i);
8+
int len2 = expand(s, i, i + 1);
9+
int len = Math.max(len1, len2);
10+
if (len > end - start) {
11+
start = i - (len - 1) / 2;
12+
end = i + len / 2;
13+
}
14+
}
15+
return s.substring(start, end + 1);
16+
}
17+
18+
private static int expand(String s, int left, int right) {
19+
while (left >= 0 && right < s.length() && s.charAt(left) == s.charAt(right)) {
20+
left--;
21+
right++;
22+
}
23+
return right - left - 1;
24+
}
25+
26+
public static void main(String[] args) {
27+
System.out.println("Longest palindrome in 'babad': " + longestPalindrome("babad"));
28+
}
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package string.manipulations;
2+
3+
public class LongestWordFinder {
4+
public static String findLongestWord(String sentence) {
5+
String[] words = sentence.split("\\s+");
6+
String longest = "";
7+
for (String word : words) {
8+
if (word.length() > longest.length()) {
9+
longest = word;
10+
}
11+
}
12+
return longest;
13+
}
14+
15+
public static void main(String[] args) {
16+
String input = "Java is an object-oriented programming language";
17+
System.out.println("Longest word: " + findLongestWord(input));
18+
}
19+
}
20+

0 commit comments

Comments
 (0)