CodingInterviewProgramsAll
CodingInterviewProgramsAll
=====================================================================
package com.InterviewPrep;
import java.util.Arrays;
import java.util.Arrays;
Arrays.sort(arr1);
Arrays.sort(arr2);
if (args.length == 0) {
System.out.println("Please provide a valid input.");
return;
}
int num = Integer.parseInt(args[0]);
int sum = 0, temp = num, digits = Integer.toString(num).length();
while (temp > 0) {
sum += Math.pow(temp % 10, digits);
temp /= 10;
}
System.out.println(sum == num ? num + " is Armstrong" : num + " is not
Armstrong");
}
}
================================
package com.InterviewPrep;
import java.util.Arrays;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.ArrayList;
import java.util.List;
for (int bs : a) {
if (bs % 2 == 0) { // Correct condition
evencount++;
evenNumbers.add(bs);
} else {
oddcount++;
oddNumbers.add(bs);
}
}
import java.util.Arrays;
}
}
=======================================
package com.InterviewPrep;
}
System.out.println("Total digits sum is : "+sum);
}
}
===================================
package com.InterviewPrep;
import java.util.HashMap;
import java.util.Map;
//approach 2
HashMap<String,Integer> hs = new HashMap<>();
for(String b : arr){
hs.put(b,hs.getOrDefault(b,0)+1);
}
for(Map.Entry<String,Integer> entry : hs.entrySet()){
if(entry.getValue()>1){
System.out.println("duplicate key is :"+ entry.getKey());
}
}
}
}
===========================================
package com.InterviewPrep;
import java.util.*;
import java.util.stream.Collectors;
import com.naresh.Employees;
import java.util.ArrayList;
import java.util.Comparator;
//Descending order
// emplist.sort((e1,e2)->e2.getName().compareTo(e1.getName()));
// emplist.forEach(System.out::println);
}
}
=====================================
package com.InterviewPrep;
import java.util.ArrayList;
import java.util.List;
// Loop through the char array and add characters to the respective lists
for (char ch : a) {
if (ch == ' ') {
spaces.add(ch); // Add spaces to the spaces list
} else {
nonSpaces.add(ch); // Add non-space characters to nonSpaces list
}
}
List<Character> result = new ArrayList<>(spaces); // Create a new list with
Spaces
result.addAll(nonSpaces); // Add Nonspaces to the list
}
}
=============================================
package com.InterviewPrep;
import java.util.Arrays;
import java.util.stream.IntStream;
import java.util.stream.Stream;
}
}
====================================
package com.InterviewPrep;
import java.util.Arrays;
import java.util.stream.IntStream;
// Extracting zeros
int[] zeros = Arrays.stream(a).filter(n -> n == 0).toArray();
// Extracting non-zero numbers
int[] nonZeros = Arrays.stream(a).filter(n -> n != 0).toArray();
import java.util.Arrays;
import java.util.stream.Stream;
System.out.println(Arrays.toString(result));
}
}
=================================
package com.InterviewPrep;
import java.util.Scanner;
import java.util.HashMap;
import java.util.Map;
public class FindFrequencyExample {
public static void main(String[] args) {
if (args.length == 0) {
System.out.println("Please provide a string as input.");
return;
}
// Input string
String str = String.join(" ", args).toLowerCase();
// Create a HashMap to store character frequencies
Map<Character, Integer> freqMap = new HashMap<>();
// Iterate through the string
for (char ch : str.toCharArray()) {
if (ch != ' ') { // Ignore spaces
freqMap.put(ch, freqMap.getOrDefault(ch, 0) + 1);
}
}
// Print the character frequencies in the desired format
freqMap.forEach((key, value) -> System.out.print(key + "" + value + " "));
}
}
=================================
package com.InterviewPrep;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
// Input string
// String str = String.join(" ", args).toLowerCase();
String str = args[0];
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
}
}
=================================
package com.InterviewPrep;
import java.util.*;
import java.util.stream.Collectors;
// approach-1
// Collection<String> cl = ma.values();
// System.out.println(cl);
//normal approach
for(Map.Entry<Integer,String> mdta : ma.entrySet() ){
var value = mdta.getValue();
System.out.println(value);
}
}
}
=============================
package com.InterviewPrep;// Java Code to check if two Strings are anagrams of
// each other using sorting
import java.util.Arrays;
class GfG {
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
var collect =
employeedata.stream().collect(Collectors.groupingBy(Employee::getDepartment,
Collectors.groupingBy(Employee::getSalary)));
}
======================================
package com.InterviewPrep;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.ListIterator;
// Iterate in reverse order for the second half (after the midpoint)
// First, find the midpoint for the second half, then reverse iterate
int index =0;
for (int i = list.size() - 1; i >= midpoint; i--) {
sechalf[index] = list.get(i); // Store the element in the array
index++;
}
System.out.println("Second half in reverse: " + Arrays.toString(sechalf));
}
}
====================================
package com.InterviewPrep;
import java.util.*;
import java.time.Year;
Year y = Year.of(i);
if(y.isLeap()){
System.out.println(y+" is leap year");
}
else {
System.out.println(y + " not a leap year");
}
}
}
===================================
package com.InterviewPrep;
import java.util.*;
import java.util.Scanner;
scanner.close();
}
}
========================================
package com.InterviewPrep;
import java.util.*;
import java.util.Arrays;
// Print the largest and second largest numbers (after sorting, they will
be the last two elements)
int max1 = v[v.length - 1]; // Largest number
int max2 = v[v.length - 2]; // Second largest number
System.out.println("The two largest numbers are: " + max1 + " and " +
max2);
}
}
=============================
package com.InterviewPrep;
import java.util.*;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;
}
}
============================================
package com.InterviewPrep;
import java.util.*;
======================================
package com.InterviewPrep;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
==========================================
package com.InterviewPrep;
//import java.util.stream.*;
import java.util.Scanner;
if(input.toLowerCase().equals(rev.toLowerCase())){
System.out.println("given input is palindrome "+ input);
}
else{
System.out.println("given input is not palindrome "+ input);
}
}
}
===================================
package com.InterviewPrep;
import java.util.stream.IntStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
List<Integer> x = l.stream().distinct().collect(Collectors.toList());
System.out.println(x);
}
}
==========================
package com.InterviewPrep;
import java.util.ArrayList;
import java.util.List;
removeduplicates((ArrayList<Integer>) l);
System.out.println("unique elements in list is "+l);
}
}
}
===========================
package com.InterviewPrep;
// int n = 987654;
// StringBuffer sb = new StringBuffer(String.valueOf(n));
// System.out.println(sb.reverse());
// int rev =0;
// while (n!=0){
// rev = rev*10+n%10;
// n = n/10;
// System.out.println("Reverse Number is "+rev);
// }
}
}
================================
package com.InterviewPrep;
import java.util.LinkedList;
ll.add(1);
ll.add(2);
ll.add(3);
ll.descendingIterator().forEachRemaining(ll1::add);
import java.util.*;
import java.util.*;
import java.util.Scanner;
else
System.out.println("Error: Length exceeds string length.");
scanner.close();
}
}
============================
package com.InterviewPrep;
import java.util.Arrays;
import java.util.Scanner;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
// approach-2
var slarge = al.stream().sorted((a,b)->b-
a).skip(1).findFirst().orElseThrow();
System.out.println(slarge);
}
}
==================================
package com.InterviewPrep;
import java.util.*;
// Sorting by values
map.entrySet().stream()
.sorted(Map.Entry.comparingByValue()) // Sort by values
.forEach(entry -> System.out.println(entry.getKey() + " = " +
entry.getValue()));
}
}
===================================
package com.InterviewPrep;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
O/P :: BADC
======================================
package com.InterviewPrep;
// a= a+b;
// b =a-b;
// a = a-b;
// System.out.print(a+" "+b);
// a= a*b;
// b =a/b;
// a = a/b;
// System.out.print(a+" "+b);
a= a^b;
b= a^b;
a= a^b;
System.out.println(a+ " "+b);
// b =a+b -(a=b);
// System.out.println(a+" "+b);
}
}
=====================
package com.InterviewPrep;
s1 = s1.concat(s2);
s2 = s1.substring(0,s1.length()-s2.length());
s1 = s1.substring(s2.length());
System.out.println(s1+ ":"+s2);
}
}
================================
package com.InterviewPrep;
import java.util.Arrays;
import java.util.List;
import java.util.Arrays;
import java.util.Collections;
// Arrays.sort(c, Collections.reverseOrder());
// System.out.println(c[0]);
// System.out.println(c[1]);
//approach-2
// Directly sort the array in descending order and take the first two
elements
int[] maxTwo = Arrays.stream(c)
.sorted((a, b) -> b - a) // Sort descending
.mapToInt(Integer::intValue) // Convert back to int
.toArray(); // Collect as an array
// System.out.println("The two largest numbers are: " + maxTwo[0] + " and "
+ maxTwo[1]);
System.out.printf("the two largest numbers are %d & %d%n",
maxTwo[0],maxTwo[1]);
}
}
==============================
package com.InterviewPrep;
String s = args[0].toLowerCase();
if (s.matches(".*[aeiou].*")) {
System.out.println("The given input contains vowels.");
} else {
System.out.println("The given input does not contain vowels.");
}
}
}
================================
// Parallelly adding Two lists into new list
package com.InterviewPrep;
import java.util.*;
import java.util.*;
public class FrequencyLettersRemove{
public static void main(String [] args){
String str = "abcabcdefghi";
Set<Character> hs = new HashSet<>();
for(char ch : str.toCharArray()){
hs.add(ch);
}
StringBuilder sb = new StringBuilder();
for(Character st : hs){
sb.append(st);
}
System.out.println(sb.toString());
}
===================================================
// Compare Two Strings WithOut Comparable and Comparator
package com.InterviewPrep;
import java.util.Objects;
if (Objects.equals(str1, str2)) {
System.out.println("Strings are equal.");
} else {
System.out.println("Strings are not equal.");
}
}
}
==================================
// Write Odd and Even Numbers Using Two Threads
package com.InterviewPrep;
oddThread.start();
evenThread.start();
}
}
===============================
// Print Names and count Using java 8
import java.util.*;
import java.util.stream.*;
public class CountWords{
public static void main(String [] args){
String [] s =
{"Anjaneya","RamaChandra","Krishna","Gopala","Mukunda","Krishna"};
Map<String,Long> res = Arrays.stream(s).collect(Collectors.groupingBy(w-
>w,Collectors.counting()));
res.forEach((w,c)->System.out.println(w+"="+c));
}
}
=============================================
// Remove duplicate from a list of employee object based on empID using streams
import java.util.*;
import java.util.stream.Collectors;
class Employee {
private int empID;
private String name;
@Override
public String toString() {
return "Employee{" + "empID=" + empID + ", name='" + name + '\'' + '}';
}
}
uniqueEmployees.forEach(System.out::println);
}
}
============================================